datascript

seepel 2024-07-25T21:35:03.345779Z

Hello again đź‘‹ I was surprised by the behavior of :db.unique/identity and I want to check if this is a bug? If I transact a random uuid identity with a constant string identity twice I would expect an error to be thrown, but instead it seems to update the random uuid identity.

(let [conn (d/create-conn {:unique/uuid {:db/unique :db.unique/identity}
                           :unique/string {:db/unique :db.unique/identity} })
      tx1 (d/transact! conn [[:db/add -1 :unique/uuid (random-uuid)]
                             [:db/add -1 :unique/string "foo"]])
      tx2 (d/transact! conn [[:db/add -1 :unique/uuid (random-uuid)]
                             [:db/add -1 :unique/string "foo"]])]
  (concat (:tx-data tx1)
          (:tx-data tx2)))
(#datascript/Datom [1
                    :unique/uuid
                    #uuid "d1489fe3-5949-4bd8-a80e-88f3c1bcc301"
                    536870913
                    true]
 #datascript/Datom [1 :unique/string "foo" 536870913 true]
 #datascript/Datom [1
                    :unique/uuid
                    #uuid "d1489fe3-5949-4bd8-a80e-88f3c1bcc301"
                    536870914
                    false]
 #datascript/Datom [1
                    :unique/uuid
                    #uuid "1ca95fc0-ba82-495f-b718-f1aa37154881"
                    536870914
                    true])

Niki 2024-07-26T08:02:19.898389Z

no that’s called upsert. It’s intentional

Niki 2024-07-26T08:02:41.485099Z

there is another unique that will throw

seepel 2024-07-26T18:00:58.601639Z

Oh, I didn’t think I could use lookup-refs with :db.unique/value but it seems like I can? Is the only difference between :db.unique/identity and :db.unique/value that one upserts and one errors, or are there other parts of the API where they behave differently?

Niki 2024-07-27T08:48:32.331619Z

I was pretty sure lookup links don’t work with value, but if you checked and they do, then they do :)