I'm getting an error in prod that is inexplicable. Given the following schema snippets:
... one type....
{:db/ident :bankaccount/id
:db/valueType :db.type/string
:db/unique :db.unique/identity
:db/cardinality :db.cardinality/one}
... another type...
{:db/ident :income/category
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one}
{:db/ident :bankaccount
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one}
This transaction periodically fails even though a bank account with the id exists.
(d/transact conn [{:db/id -1
:income/category "Salaries"
:bankaccount [:bankaccount/id "my-unique-id"]}])@alekcz360 Could you try 1562 or similar older release just to make sure it is not a regression?
This is weird. Have you used lookup-refs before in other contexts? I don't see immediately what the problem is.
Caches should not affect this at all, they only should affect performance.
If you could provide a reproducible example that would be great!
My problem is that I can't reproduce it.
It's happening to about 15 customers in prod
I have more information that leads me to think it's to do with indexing
This query runs for a long time them returns a value:
(dh/q '[:find (pull ?e [*]) :in $ :where [?e _ "bank139289550840960000"]] @conn)But this query returns nothing:
(dh/q '[:find (pull ?e [*]) :in $ :where [?e :bankaccount/id "bank139289550840960000"]] @conn)And it's not the new releases I was on 0.6.1558 and I upgraded to 0.6.1568 and it still happens
Can you return which attribute is matched in the first query that returns the value?
(dh/q '[:find ?a :in $ :where [?e ?a "bank139289550840960000"]] @conn)@whilo this is bizarre right?
Yes, this looks strange.
Did you get other evidence that it would have something to do with the indices?
Or anything else you have observed.
When I ran this one:
(dh/q '[:find (pull ?e [*]) :in $ :where [?e _ "bank139289550840960000"]] @conn)It took really long to run
Maybe like 30 or 40 seconds
If i use a different id that is marked as unique it takes like 2 seconds
(dh/q '[:find (pull ?e [*]) :in $ :where [?e _ "i69biCzvb6dOhdL77xOhUG77b2J2"]] @conn)I don't understand indices and how they work but it's almost as if bankaccount/id is deemed to be a "primary key" for lack of a better word
The entity-id of [:bankaccount/id "bank139289550840960000"] is 37172
But this:
(dh/pull @conn '[*] [:bankaccount/id "bank139289550840960000"])
throws an error
ERROR [datahike.db.utils:141] - Nothing found for entity id [:bankaccount/id "bank139289550840960000"] {:error :entity-id/missing, :entity-id [:bankaccount/id "bank139289550840960000"]}
Execution error (ExceptionInfo) at datahike.db.utils/entid-strict (utils.cljc:141).
Nothing found for entity id [:bankaccount/id "bank139289550840960000"]That is weird. Is there a way to somehow make this reproducible?
How big are the indices?
You could export each one of them and then look at them directly.
How would I do that?
I'm not entirely sure what you mean by how big the indices are.
There are 2255108 datoms and about 23 :db/ident marked as unique
You can use the datom function to just get a sequence of datoms for each index.
So if I understand correctly i basically run:
(dh/datoms @conn {:index :avet :components [:bankaccount/id]})And then look through the datoms
I think you can do (dh/datoms @conn :eavt)
I tried that. Haha not enough heap space
This returned a whole bunch of bank accounts
(dh/datoms @conn {:index :avet :components [:bankaccount/id]})Basically valid datoms
But not the one giving issues
It is a lazy sequence you can filter before loading. e.g. by attribute.
Ok.
How is it possible for bank139289550840960000 this specific id not to be index but 312 others are?
Is it not in the index?
I would compare eavt and aevt and avet if you have it for this attribute.
yeah it's not in the index. If I pull the ones in the index there are 312 of them.
But if I filter the datoms by those with attribute bankaccount/id there are 417 of them
Is it in one of the others?
Oh, that is weird.
Which two calls exactly differ?
It is missing in:
(dh/datoms @conn {:index :avet :components [:bankaccount/id]})And present in:
(dh/datoms @conn {:index :aevt :components [:bankaccount/id]})Missing in :avet Present in :aevt
Do you think setting :db/index true in the schema would fix the issue
But then none of the bankaccount/id values should be in there, right?
Did the schema maybe change(?)
While you were adding data. I think it should reject that, but maybe it didn't.
I had to rebuild my db from datom export. 2 months ago. But no schema changes to this attribute have happen recently and the errors started 2 days ago
So before two days ago bank139289550840960000 was found?
Sorry, still trying to understand what the state is.
Yes. 140 other entities created by this user have links to bank139289550840960000
Which means it was working
Has antyhing changed in the last days with the setup or data?
Not in setup. But we went from having 1M datoms to 2M datoms
Is it hard for you to export and import and see whether the problem goes away? That would validate your claim that it would be an indexing problem.
I can try that. Lemme give it a shot locally. And see. I'll feed back tomorrow.
Thanks for the help
Also if it was possible to reproduce the change that happened over the last few days and see when it disappears through bissection that would be the best way to track it down.
Yeah, sure. It should work.
There could be a bug somewhere, we just need to pin it down more.
I loaded the datoms locally and they were all there. Nothing was missing. avet and aevt contained equal datoms when filtered by bankaccount/id
Reloading the datoms work to rebuild the index
It did lead me to a world of pain though
I loaded raw datoms as entities.
And now certain datoms aren't returned in queries
That is not good. I would like to understand what happened, but it is very difficult for me to know what is going on without being able to reproduce it.
Specifically Nothing found for entity id [:bankaccount/id "my-unique-id"]
Could the size of search or store cache play a role?
This release includes a minor fix to allow sets to be passed to pull expressions, improving Datomic compatibility. Thanks to @uppfinnarjonas!