Update and Upsert for both Vertex and Edge - orientdb

Is there a way to create both a Vertex and Edge in the same query?
I am aware that we can use out_EdgeName/ in_EdgeName to update the edge of a vertex if it already exists in an UPDATE query, but how to do that so that a new edge is created and assigned to the vertex?
Example use case in an Update Upsert query - a Vertex is being created and we require a new Edge to be created for that vertex. Can we do it in the same query or we would need 2 queries at least for that (i.e 2 UPDATE - UPSERTS)?
Taking cue from orientdb sql update edge? :
Something like - UPDATE Persons SET phone=000000, out_Inside=(
UPDATE Edge UPSERT where in=#some_rid/sub-query, out=$this) where person_id=8

This question is actual if you are using new version of orientdb 2.1.
But as far as i know this features was implemented in 2.2 version.
"As far as I can tell, update works (including in/out):"
UPDATE FRIEND SET in=#11:5 WHERE in.name="Samantha" and out.name="Mat"
Although, using a query inside the set clause for in/out will cause it to return array:
UPDATE Friend SET in=(SELECT FROM User WHERE name="Jason") WHERE in.name="Samantha" and out.name="Mat"
Upsert also works, although when creating a new vertex it doesn't set the in/out properties.
You could set the in/out properties your self, like this:
UPDATE Friend SET comment="Wazzzaup", in=#11:5, out=#11:6 UPSERT WHERE in.name="Jason" AND out.name="Mat"
It will result in a longer query when using sub-query for in= and out=, but at least it works (sub-query has same problem as above).
I got this information from issue:
https://github.com/orientechnologies/orientdb/issues/1114#issuecomment-156585776

Related

OrientDB - Create Edges if not exist with subselect

I have a unique constraint on edge:
CREATE CLASS hasAssignee extends E
CREATE PROPERTY hasAssignee.out LINK Assignable
CREATE PROPERTY hasAssignee.in LINK User
CREATE INDEX UniqueHasAssignee ON hasAssignee(out,in) UNIQUE
I want to multiple create edges if they don't yet exist in one query. If they do exist, then either replace them, or simply don't add them. Something like that, but without possibility of errors:
CREATE EDGE hasAssignee FROM ( SELECT FROM Project WHERE id=:id ) TO ( SELECT FROM User WHERE login in :login )
UPSERT from what I've noticed can deal with only one edge at a time (at least I couldn't produce query parsable by studio)
I see a few possibe solutions (but I don't see anything like it in docs):
something like CREATE IFNOTEXISTS
Soft indexes, that don't throw an error, simply don't create additional edges
Remove unique index, create duplicates, and somehow remove duplicates after each query (?)
Is it possible to do something like that in orientdb? Should I fill out a request?
There is an open feature request for this:
https://github.com/orientechnologies/orientdb/issues/4436
My suggestion is to vote on it

Add multiple properties during edge creation in gremlin

I have used this query
g.V().has('empId','123').as('a').V().has('deptId','567').addE('worksAt').properties('startedOn','17/15/07','title','manager','pay',15000)
which doesn't work.
Adding a single property works with a .property step
g.V().has('empId','123').as('a').V().has('deptId','567').addE('worksAt').property('startedOn','17/15/07')
Instead of specifying all properties in one property-step, you can simply concatenate multiple property-steps:
g.V().has('empId','123').as('a').V().has('deptId','567').addE('worksAt').
property('startedOn','17/15/07').property('title','manager').property('pay',15000)
Your query doesn't specify which vertices should be connected by the edge. When you want the edge to go out from the vertex with the empId 123, then you have to insert a from:
g.V().has('empId','123').as('a').V().has('deptId','567').addE('worksAt').from('a').
property('startedOn','17/15/07').property('title','manager').property('pay',15000)
See the addEdge-step for more information.

upserting data in TitanDB

I am using TitanDB with Cassandra as storage and ElasticSearch as Index. I found out that everytime you add Vertex in TitanDB, it generates a unique identifier.
All the elements I am adding into it, has already an identifier, this has been added as property of the Vertex.
My question is:
If I will add again a Vertex with the same id, How does TitanDB recognise that it is a duplicate?
Is it possible update element on duplicate key ? Or you have first to make a query within TitanDB? If so, isn't it a terrible waste of time doing so?
There is no direct method for "upsert". As noted above, in the comment on the question, the "getOrCreate" approach is the standard way to do this. So, "yes" you would need to do a lookup via index on your identifier property.
Titan can detect duplicates if you establish your indexed property with a unique constraint:
mgmt = graph.openManagement()
name = mgmt.getPropertyKey('name')
mgmt.buildIndex('byNameUnique', Vertex.class).addKey(name).unique().buildCompositeIndex()
mgmt.commit()
If the same property value is applied twice now, an exception will be generated on commit of the transaction. Use unique indexes wisely as they will affect performance especially if you expect heavy contention on the property that the unique is applied to.

Can't query edges in OrientDB anymore

When I try to query:
select out('teaches') from Course
OrientDB returns empty lists, while the edges that I'm trying to find do exist.
Also, in the graph editor, the edges options won't show up anymore:
Normally, I can choose edges by hovering over the arrows. Now it doesn't do anything.
I've noticed that this started to happen when I started indexing my edges with a unique hash key. Does this have anything to do with it?
I'm using OrientDB version 2.0.5.
Apparently, it didn't work because I was using an UPDATE command.
UPDATE teaches SET in = #12:1, out = #13:1 UPSERT WHERE in = #12:1 AND out = #13:1 will create the edge (record) if it does not yet exist, but you won't be able to query it.
CREATE EDGE teaches from #13:1 to #12:1 does work and you will be able to query it.
Not sure whether this can be considered a bug...

SqlDataAdapter Update

Can any one help me why this error occurs when i update using sqlDataadapter with join query
Dynamic SQL generation is not supported against multiple base tables.
You have a "join" in your main query for your dataset (The first one in the TableAdapter with a check by it). You can't automatically generate insert/update/delete logic for a TableAdapter when the main query has multiple tables referenced in the query via a join. The designer isn't smart enough to figure out which table you want to send updates to in that case, that is why you get the error message.
Solution. Ensure that your main query only references the table you want the designer to write insert/update/delete code for. Your secondary queries may reference as many tables as you want.
It was in the case that i was trying to set value for identity column in my datarow. Simply i deleted the code to set value for identity column and it will work.
My Scenario:
Database:
uin [primary, identity]
name
address
Whenever i tried to set the datarow("uin") the error occurs. But works fine with datarow("name") and datarow("address").
Hope it works for you too