«

Defining Attributes

Attributes are defined using the same data model used for application data. That is, attributes are themselves defined by entities with associated attributes.

The following built-in attributes are required to define a new attribute:

  • :db/ident, a name that is unique within the database
  • :db/cardinality, specifying whether entities can have one or a set of values for the attribute
  • :db/valueType, the type allowed for an attribute's value

In addition to these required attributes, there are many optional attributes that can be used to further define your attributes. For a full list of available built-in attributes, see the Schema Data Reference.

Leading underscores are used for reverse lookup in pull.

If your attribute has a leading underscore then you will not be able to use reverse lookup with that attribute.

You define new attributes by submitting a data structure to Datomic as part of a transaction. All the required information for an attribute must be specified in a single transaction, after which the attribute will be immediately available for use.

It is idiomatic to use the transaction map data structure to define attributes. The example below defines an attribute named :person/name of type :db.type/string with :db.cardinality/one that is intended to capture a person's name.

(def name-schema
  [{:db/ident :person/name
    :db/valueType :db.type/string
    :db/cardinality :db.cardinality/one
    :db/doc "A person's name"}])

;; using a connection named conn:
(d/transact conn {:tx-data name-schema})

;; now you can transact names
(d/transact conn {:tx-data [{:person/name "Grace Hopper"}]})

Next: Schema Reference