从实体中收回 :db/ident
retracting :db/ident from an entity
我有一堆特殊的实体,但它们不是数据库模式的一部分。由于这些实体很特殊,我为它们设置了一些 :db/ident
属性,以便在我的程序中轻松访问它们。
假设我调用了这些帐户之一 :base-account
现在的问题是,当我使用实体 api 访问这些实体时,我遇到了这个问题:
;; access some entity that references one of the special entities
> (d/touch (d/entity db 12345678))
==>
{:transaction/amount 22334455,
:transaction/from {:db/id 0987654}, ;; normal reference to an entity
:transaction/to :base-account} ;; this is a reference to a special account with a :db/ident attribute
这导致我之前编写的一些代码出现问题,因为这不会给我 :transaction/to
帐户的详细信息。
因此,为了解决这个问题,我从这些实体中删除了 :db/ident
属性:
> (d/transact connection [[:db/retract id-of-the-special-account
:db/ident :base-account]])
成功从实体中删除 :db/ident
:
> (:db/ident (d/entity db id-of-the-special-account))
==> nil
但出于某种原因(可能是错误),实体 api 调用仍然使用其旧标识引用它:
> (d/entity db :base-account) ;; should not work
==> {:db/id id-of-the-special-account}
那么如何才能从这些实体中删除身份,而不必将它们从数据库中完全删除?或者也许是一种以理智的方式修复 (d/entity ....)
调用工作方式的方法?
编辑:我正在使用 datomic-pro-5544
Idents should be used for two purposes: to name schema entities and to
implement enumerated tags. Both of these uses are demonstrated in the
introductory tutorial. To support these usages, idents have two
special characteristics:
- Idents are designed to be extremely fast and always available. All idents associated with a database are stored in memory in every
Datomic transactor and peer.
- When you navigate the entity API to a reference that has an ident, the lookup will return the ident, not another entity.
最后一个要点可能会影响您。
下一段:
These characteristics also imply situations where idents should not be
used:
- Idents should not be used as unique names or ids on ordinary domain entities. Such entity names should be implemented with a
domain-specific attribute that is a unique identity.
- Idents should not be used as names for test data. (Your real data will not have such names, and you don't want test data to behave
differently than the real data it simulates.)
由此看来您可能想要重新设计数据库,而不是尝试取消对 :db/ident
的使用。
我有一堆特殊的实体,但它们不是数据库模式的一部分。由于这些实体很特殊,我为它们设置了一些 :db/ident
属性,以便在我的程序中轻松访问它们。
假设我调用了这些帐户之一 :base-account
现在的问题是,当我使用实体 api 访问这些实体时,我遇到了这个问题:
;; access some entity that references one of the special entities
> (d/touch (d/entity db 12345678))
==>
{:transaction/amount 22334455,
:transaction/from {:db/id 0987654}, ;; normal reference to an entity
:transaction/to :base-account} ;; this is a reference to a special account with a :db/ident attribute
这导致我之前编写的一些代码出现问题,因为这不会给我 :transaction/to
帐户的详细信息。
因此,为了解决这个问题,我从这些实体中删除了 :db/ident
属性:
> (d/transact connection [[:db/retract id-of-the-special-account
:db/ident :base-account]])
成功从实体中删除 :db/ident
:
> (:db/ident (d/entity db id-of-the-special-account))
==> nil
但出于某种原因(可能是错误),实体 api 调用仍然使用其旧标识引用它:
> (d/entity db :base-account) ;; should not work
==> {:db/id id-of-the-special-account}
那么如何才能从这些实体中删除身份,而不必将它们从数据库中完全删除?或者也许是一种以理智的方式修复 (d/entity ....)
调用工作方式的方法?
编辑:我正在使用 datomic-pro-5544
Idents should be used for two purposes: to name schema entities and to implement enumerated tags. Both of these uses are demonstrated in the introductory tutorial. To support these usages, idents have two special characteristics:
- Idents are designed to be extremely fast and always available. All idents associated with a database are stored in memory in every Datomic transactor and peer.
- When you navigate the entity API to a reference that has an ident, the lookup will return the ident, not another entity.
最后一个要点可能会影响您。
下一段:
These characteristics also imply situations where idents should not be used:
- Idents should not be used as unique names or ids on ordinary domain entities. Such entity names should be implemented with a domain-specific attribute that is a unique identity.
- Idents should not be used as names for test data. (Your real data will not have such names, and you don't want test data to behave differently than the real data it simulates.)
由此看来您可能想要重新设计数据库,而不是尝试取消对 :db/ident
的使用。