Fork me on GitHub
#asami
<
2022-07-12
>
Mattias09:07:57

So, on my (long) way to grokking Asami, I’m trying to understand IDs. I’m trying to do my homework, but a quick question - how does Asami figure out a value represents an ID? (For now, the way out I see is that Asami doesn’t care when storing data, only when resolving queries - but that still leaves temporary IDs. How does Asami figure out I don’t just want the regular negative numer as a property value?)

Mattias09:07:29

In case I’m just noobishly off target here, I’ll keep this in a thread. I tried transacting this part from the docs:

{:db/id -1
 :inventory/label "Widget"
 :inventory/part-nr "THX-1138"
 :inventory/stock-count 2187}
{:db/id -2
 :inventory/label "Doohicky"
 :inventory/part-nr "AA-23"
 :inventory/stock-count 5
 :inventory/replaces -1}
[:db/add -1 :inventory/replaced-by -2]

Mattias09:07:15

The result as shown in the docs is:

:a/node-1031 :inventory/label "Widget"
:a/node-1031 :inventory/part-nr "THX-1138"
:a/node-1031 :inventory/stock-count 2187
:a/node-1032 :inventory/label "Doohicky"
:a/node-1032 :inventory/part-nr "AA-23"
:a/node-1032 :inventory/stock-count 5
:a/node-1032 :inventory/replaces :a/node-1031
:a/node-1031 :inventory/replaced-by :a/node-1032

Mattias09:07:43

While I see this:

[:a/node-24040 :inventory/label "Widget"]
 [:a/node-24040 :inventory/part-nr "THX-1138"]
 [:a/node-24040 :inventory/stock-count 2187]
 [:a/node-24040 :db/ident :a/node-24040]
 [:a/node-24040 :a/entity true]
 [:a/node-24040 :inventory/replaced-by :a/node-24041]
 [:a/node-24041 :inventory/label "Doohicky"]
 [:a/node-24041 :inventory/part-nr "AA-23"]
 [:a/node-24041 :inventory/stock-count 5]
 [:a/node-24041 :inventory/replaces -1]
 [:a/node-24041 :db/ident :a/node-24041] 
 [:a/node-24041 :a/entity true]

Mattias09:07:51

I’m missing something fundametal about how (temporary) IDs work. Pointers appreciated 🙂

quoll14:07:13

Nope… you read it right. The whole negative-number-in-an-entity thing doesn’t work right. The way around it was to use :db/add but with Jakub’s update that can have problems too 😕 I want to introduce schemas (both temporary and stored) that can solve this. This would allow attributes to say if they reference a number or another entity. That would solve the problem completely.

Mattias14:07:44

Ah, right. Makes sense. Hoping you can find a compromise, I find the schemaless world both freeing and productive. Anyway, thanks for clearing that bit up! 🙂

quoll14:07:26

The idea is that a schema could be either inserted or added as a parameter to a transact (since it accepts a map with numerous keys already). This could allow attribute descriptions for the attributes you want to treat a particular way, and just treat everything else as usual

💯 1
Cora (she/her)15:07:38

partial schemas is a neat idea, I like it

🙏 1
1
Mattias16:07:27

Sounds brilliant, exciting to see where it goes 🙂

Jakub Holý (HolyJak)18:07:08

I guess if you replaced :inventory/replaces -1 in the entity map with a separate [:db/add -2 :inventory/replaced-by -1] it would work, no?

quoll19:07:44

It should… unless the -1 had been used as a temporary entity ID already

Mattias19:07:46

Just to show my ignorance, how does using -1 with a :dn/add vector tell Asami its a reference and not a regular plain number?

quoll19:07:16

If the attribute in a :db/add is :db/id then you’re explicitly telling the transaction that it should treat the value as an alias for the entity. If that’s a negative number, then that means it’s a temporary entity ID. All subsequent statements that include that temporary entity ID will have it replaced with the actual entity. This has the potential to cause problems. I realize that. It’s come about through some recent changes. Rather than just not allowing the changes, this ambiguity is being allowed until schemas get introduced.

quoll19:07:46

If you haven’t used a negative number as an ID, then a :db/add will just see it as a negative number, and you’ll be fine

Mattias19:07:52

With this:

[:db/add -1 :inventory/replaced-by -2]

Mattias19:07:07

How does Asami know -2 is an ID and not just the attribute -2? 🙂

Mattias19:07:52

(There is no :db/id right there?)

quoll19:07:32

In that case, it’s the number -2

Mattias19:07:47

That’s out of context, but from the docs at: https://github.com/quoll/asami/wiki/4.-Transactions

quoll19:07:25

If you had said:

[:db/add -2 :db/id -2]
[:db/add -1 :inventory/replaced-by -2]
Then it would be an entity

quoll19:07:58

Ah, I see your example

quoll19:07:26

But do you see how the -2 was already associated with an entity?

{:db/id -1
 :inventory/label "Widget"
 :inventory/part-nr "THX-1138"
 :inventory/stock-count 2187}
{:db/id -2
 :inventory/label "Doohicky"
 :inventory/part-nr "AA-23"
 :inventory/stock-count 5
 :inventory/replaces -1}
[:db/add -1 :inventory/replaced-by -2]

Mattias19:07:31

Ok, my fault for copying just that part then. Ok, so you mean all negative numbers that have been used as IDs are thereafter used as references in the same transaction?

quoll19:07:23

That’s 3 things. An entity that gets aliased to -1, and entity that gets aliased to -2, and a statement that uses both of those aliases. Unfortunately, once those aliases are set, then you lose the ability to use those negative numbers!

quoll19:07:34

Which is a problem! I acknowledge that. It needs to be fixed

Mattias19:07:07

I see. Thank you, you have more patience explaining than I deserve. Took me awhile but now I’m there 😊

quoll19:07:34

Oh no… the thing about computers is that there is no universal truth, like in math. Everything was a choice that someone made at some point. Which means trying to learn and understand where they were coming from, and trying to identify the consistent ideas in it

💯 1