This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-09-29
Channels
- # announcements (6)
- # babashka (23)
- # beginners (15)
- # biff (15)
- # calva (17)
- # clara (5)
- # clj-kondo (41)
- # cljdoc (2)
- # cljs-dev (67)
- # cljsrn (18)
- # clojure (19)
- # clojure-europe (25)
- # clojure-nl (2)
- # clojure-norway (9)
- # clojure-uk (2)
- # clojurescript (26)
- # core-typed (6)
- # cursive (15)
- # data-science (30)
- # datahike (1)
- # datomic (18)
- # docker (6)
- # emacs (10)
- # events (2)
- # graalvm (15)
- # graphql (5)
- # hugsql (4)
- # jobs-discuss (1)
- # joker (7)
- # lsp (36)
- # malli (28)
- # off-topic (46)
- # other-languages (1)
- # pathom (5)
- # pedestal (6)
- # polylith (5)
- # reitit (2)
- # releases (1)
- # rewrite-clj (63)
- # shadow-cljs (7)
- # spacemacs (16)
- # squint (6)
- # tools-deps (6)
- # xtdb (13)
I'd like to know if there are any guarantees around the ordering of multiple values for a binding in a query. For example in this query there could be 1 or 2 values for ?value
and I am assuming that the order of those values matches the order of the rules in the or
clause - so that the value of :some/attr1
will always be returned when present, falling back to the value of :some/attr2
when not.
(d/q '[:find ?value .
:in $ ?attr
:where
[?attr-id :db/ident ?attr]
(or
[?attr-id :some/attr1 ?value]
[?attr-id :some/attr2 ?value])]
db :test/attr)
Thanks! My initial tests were working the way I hoped but eventually I found some attributes where the order was different.
Out of curiosity are there any Datomic docs or datalog articles I can read to understand that?
It’s so foundational an idea that I’m not sure it’s written down. But if it is I would expect it in the query tutorial or reference
Just for future reference, I think this is what I was looking for.
(d/q '[:find ?value .
:in $ ?attr
:where
[?attr-id :db/ident ?attr]
(or
(and [?attr-id :some/attr1 ?value]
(not [?attr-id :some/attr2]))
[?attr-id :some/attr2 ?value])]
db :test/attr)
Hey there, does anyone know if there was any response or resolution to this https://forum.datomic.com/t/troubles-with-upsert-on-composite-tuples/1355? My team is running into issues when using composite tuples with a ref as an attribute:
(def example-schema
[{:db/ident :my-first-string-attribute
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one}
{:db/ident :my-second-string-attribute
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one}
{:db/ident :my-ref-attribute
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one}
{:db/ident :my-keyword-attribute
:db/valueType :db.type/keyword
:db/cardinality :db.cardinality/one}
{:db/ident :composite-tuple/my-string-attribute+my-ref-attribute
:db/valueType :db.type/tuple
:db/tupleAttrs [:my-first-string-attribute :my-ref-attribute]
:db/cardinality :db.cardinality/one
:db/unique :db.unique/identity}
{:db/ident :composite-tuple/my-string-attribute+my-keyword-attribute
:db/valueType :db.type/tuple
:db/tupleAttrs [:my-second-string-attribute :my-keyword-attribute]
:db/cardinality :db.cardinality/one
:db/unique :db.unique/identity}
{:db/ident :a-ref}])
(def example-docs
[{:my-first-string-attribute "A String"
:my-ref-attribute :a-ref}
{:my-second-string-attribute "Another String"
:my-keyword-attribute :a-keyword}])
(def db-uri "datomic:")
(d/create-database db-uri)
(def conn (d/connect db-uri))
(d/transact conn example-schema)
(d/transact conn example-docs)
(d/entity (d/db conn)
[:composite-tuple/my-string-attribute+my-ref-attribute ["A String" :a-ref]])
;=> nil
(d/entity (d/db conn)
[:composite-tuple/my-string-attribute+my-keyword-attribute ["Another String" :a-keyword]])
;=> #:db{:id 17592186045432}
just in case you hadn’t tried it:
(d/entity (d/db conn)
[:composite-tuple/my-string-attribute+my-ref-attribute ["A String" (:db/id (d/entity (d/db conn) :a-ref))]])
=> #:db{:id 17592186045419}
when looking up the entity by a tuple id, it seems that datomic doesn’t try to turn values inside the tuple into an entity via lookup, even if we’ve described that position in the tuple as being a ref.
That seems like a relatively easy bug to fix to me.I have no idea what the lookup code actually looks like, of course, but if the identity is a tuple, then walking the data provided as a value for that identity should be straightforward.
@jackmoch Did you find a solution for this? Hitting the same problem with composite tuples + ref attrs here. Also it seems one can't update an entity by only asserting the individual ref attrs, one must also assert the composite attr which contradicts the documentation.
@U70QFSCG2 We ended up pushing the entity resolution as far down the stack as possible so the callers can pass around the tuple and it gets resolved just before the query, pull, etc is executed It definitely didn’t feel like the cleanest resolution so we also got rid of one usage pattern completely and are mainly using them for enforcing uniqueness on composite attrs I did hear back from the Datomic team and they’re aware and considering options but I’m not sure what that looks like in practice. I also did see a mention of lookup refs in the newest datomic changelog but I haven’t tried the new version yet to see if it impacts any of this behavior
We also wish for smarter lookup refs, to simplify composite key situations. I still have the feeling, that I might be doing something wrong and that's why I'm in need of such a feature, but not sure how to simplify our data model... 😕
Fwiw we're moving from composite keys, which would otherwise naturally arise in the data model, to synthetic keys (uuids) and manual checks in the pre-transact code to enforce the unique composite relationship. It's not as pretty or elegant but results in more straightforward and readable code.
We had to resort to {:my-entity/composite-id (pr-str [id-1 id-2])}
to get upsert, instead of using tuples.
i also ended up writing some transactor functions to achieve upsert behaviour, when i was using composite keys. really feels like im doing something wrong... we are dealing with 3rd-party data, which is partitioned by external users and tenants/orgs too, so most of our entities have composite key with 1. a ref attribute to the 3rd-party org entity 2. a 3rd-party ID, which is unique within the 3rd-party org, but not across orgs necessarily i would think, that this must be a pretty common scenario, so im a bit surprised how clunky is it at the end to work with it. would be pretty nice though, if the lookup refs would be more nestable...
We had the same issue with refs in tuples, and had to find some other solution for it. Would be interested to hear if there is any progress on this.