asami 2022-07-12

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?)

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]

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

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]

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

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.

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! 🙂

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

partial schemas is a neat idea, I like it

➕ 1
🙏 1

Sounds brilliant, exciting to see where it goes 🙂

Jakub Holý (HolyJak) 2022-07-12T18:28:08.169559Z

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?

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

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?

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.

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

With this:

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

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

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

In that case, it’s the number -2

If you had said:

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

Ah, I see your example

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]

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?

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!

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

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

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