将属性添加到现有数据模式
Add attribute to existing datomic schema
我正在尝试将属性添加到现有数据模式,新属性是
{:db/id #db/id[:db.part/db]
:db/ident :user-deets/enriched
:db/valueType :db.type/boolean
:db/cardinality :db.cardinality/one
:db.install/_attribute :db.part/db}
当我尝试将其作为交易提交时(如 http://docs.datomic.com/schema.html 所述),使用以下内容
(datomic/query '[{:db/id #db/id[:db.part/db]
:db/ident :user-deets/enriched
:db/valueType :db.type/boolean
:db/cardinality :db.cardinality/one
:db.install/_attribute :db.part/db}] (database/get-db))
我收到一条错误消息,指出我的查询中没有 :find 子句。
我应该如何提交此事务才能将属性添加到我的数据数据库架构中?
您的代码无法正常工作,因为您使用了错误的函数。
您想使用 transact
See doc.
(datomic/transact connection [{:db/id #db/id[:db.part/db]
:db/ident :user-deets/enriched
:db/valueType :db.type/boolean
:db/cardinality :db.cardinality/one
:db.install/_attribute :db.part/db}])
为了更轻松地创建属性和使用其他 Datomic 功能,您不妨尝试 the Tupelo Datomic library。它将允许您创建这样的属性:
(:require [tupelo.datomic :as td])
; Create some new attributes. Required args are the attribute name (an optionally namespaced
; keyword) and the attribute type (full listing at http://docs.datomic.com/schema.html). We wrap
; the new attribute definitions in a transaction and immediately commit them into the DB.
(td/transact *conn* ; required required zero-or-more
; <attr name> <attr value type> <optional specs ...>
(td/new-attribute :person/name :db.type/string :db.unique/value) ; each name is unique
(td/new-attribute :person/secret-id :db.type/long :db.unique/value) ; each secret-id is unique
(td/new-attribute :weapon/type :db.type/ref :db.cardinality/many) ; one may have many weapons
(td/new-attribute :location :db.type/string) ; all default values
(td/new-attribute :favorite-weapon :db.type/keyword )) ; all default values
在您的情况下,这将简化为
(td/transact (database/get-db)
(td/new-attribute :user-deets/enriched :db.type/boolean))
我正在尝试将属性添加到现有数据模式,新属性是
{:db/id #db/id[:db.part/db]
:db/ident :user-deets/enriched
:db/valueType :db.type/boolean
:db/cardinality :db.cardinality/one
:db.install/_attribute :db.part/db}
当我尝试将其作为交易提交时(如 http://docs.datomic.com/schema.html 所述),使用以下内容
(datomic/query '[{:db/id #db/id[:db.part/db]
:db/ident :user-deets/enriched
:db/valueType :db.type/boolean
:db/cardinality :db.cardinality/one
:db.install/_attribute :db.part/db}] (database/get-db))
我收到一条错误消息,指出我的查询中没有 :find 子句。
我应该如何提交此事务才能将属性添加到我的数据数据库架构中?
您的代码无法正常工作,因为您使用了错误的函数。
您想使用 transact
See doc.
(datomic/transact connection [{:db/id #db/id[:db.part/db]
:db/ident :user-deets/enriched
:db/valueType :db.type/boolean
:db/cardinality :db.cardinality/one
:db.install/_attribute :db.part/db}])
为了更轻松地创建属性和使用其他 Datomic 功能,您不妨尝试 the Tupelo Datomic library。它将允许您创建这样的属性:
(:require [tupelo.datomic :as td])
; Create some new attributes. Required args are the attribute name (an optionally namespaced
; keyword) and the attribute type (full listing at http://docs.datomic.com/schema.html). We wrap
; the new attribute definitions in a transaction and immediately commit them into the DB.
(td/transact *conn* ; required required zero-or-more
; <attr name> <attr value type> <optional specs ...>
(td/new-attribute :person/name :db.type/string :db.unique/value) ; each name is unique
(td/new-attribute :person/secret-id :db.type/long :db.unique/value) ; each secret-id is unique
(td/new-attribute :weapon/type :db.type/ref :db.cardinality/many) ; one may have many weapons
(td/new-attribute :location :db.type/string) ; all default values
(td/new-attribute :favorite-weapon :db.type/keyword )) ; all default values
在您的情况下,这将简化为
(td/transact (database/get-db)
(td/new-attribute :user-deets/enriched :db.type/boolean))