Fork me on GitHub
#datomic
<
2023-05-16
>
enn13:05:33

The docs give this example of using a composite tuple attribute with :db.unique/identity to get upsert behavior: > A given course/semester/student combination is unique in the database. To model this, you can create a composite tuple whose :db/tupleAttrs are >

{:db/ident :reg/semester+course+student
>  :db/valueType :db.type/tuple
>  :db/tupleAttrs [:reg/course :reg/semester :reg/student]
>  :db/cardinality :db.cardinality/one
>  :db/unique :db.unique/identity}
> With this composite installed, Datomic’s unique identity will ensure that all assertions about a semester/course/student combination resolve to the same entity. But when I try to transact an entity to get an upsert based on tuple uniqueness, I get a conflict instead of the expected upsert behavior:
:db.error/unique-conflict Unique conflict: :notion-issued-token/user+notion-workspace, value: [334251539360417 "8e3f6da9-296a-403a-9364-6317fa204d1c"] already held by: 338649628381186 asserted for: 338649628381535
Am I misunderstanding how this is supposed to work, or is this a bug? If it’s relevant, this attribute was previously :db.unique/value before being changed to :db.unique/identity.

enn14:05:13

I see two related Datomic forum links but they leave me only more confused. This one says that you must explicitly assert the composite tuple attribute (not just its constituent attributes) to get the upsert behavior: https://forum.datomic.com/t/db-unique-identity-does-not-work-for-tuple-attributes/1072. This one says that you cannot explicitly assert the composite tuple attribute: https://forum.datomic.com/t/troubles-with-upsert-on-composite-tuples/1355. Both are several years old so I don’t know if they are still applicable.

jjttjj14:05:57

I think long story short you just have to provide the tupleattrs yourself when transacting an upsert

jjttjj14:05:03

Or use a transaction function to handle the "upsert"

enn14:05:53

Ah, thank you for that link. Seems like it might be related to lookup refs. I think I’m starting to be convinced by Francis’s “just use a tx fn” approach. 🙂