I found that upsert does not use the @upsert index and can be used normally. What is the purpose of the @upsert index?
I see that several users get confused with this. We have a lot of conversation about this here on discuss. The difference is that the Upsert Block is a type of query transaction that does two or more operations to upsert some data. The upsert directive is totally different thing. It is used in Upsert Procedure to avoid duplicates inputs** at transaction level. But it is a bit complex to use, cuz it is not in the Query system. It is a procedure that the user have to deal on his end.
Upsert directive has nothing to do with Upsert Block. And there’s no need to use both of them at the same time. Actually makes no sense doing so.
Can you elaborate more？Still a little confused！ My understanding is: The purpose of the Upsert block is to prevent concurrent writes to duplicate nodes. Is @upsert a unique constraint?
No, Upsert block isn’t to prevent anything. It is a type of query that you can mitigate the duplication issue. But it can be user for several things. Like Bulk Upsert hundreds of fields in a single query.
Not exactly. But it will work like that. It tries to check the index, every time a new transaction bumps into concurrency. It also won’t prevent duplicates (I mean “duplicate nodes”) as far I know. It will try to avoid them if you use correctly. It is more like “If two transactions tries to modify the same input, discard the last one”. Something like that. (I used this a long time ago, I don’t even remember for sure. But it’s kind of useless today as we have Upsert Block)
In the Upsert Procedure (the one that uses the
@Upsert directive) - At your client side (your code) you have to do two queries. One to find the UID and other to mutate. So basically it is a “handcrafted” Upsert that checks the inputs on the fly.
The upsert block is way better for duplicates. And easier to avoid them.