Catalin Vladu
Catalin Vladu

Reputation: 389

orient db create vertex

I want to create some vertices in Orientdb. In SQL, syntax is something like:

insert into table1 (col1, col2, foreignKey1) select column1, column2, id from table2

Relationship between table1 and table2 is one-to-one. I want to do a similar thing in OrientDB (with foreignKey1 I'll create edges)

create vertex table1 <select [...] from table2>
create edge from <@rid of the new created vertex> to <@rid from table2>

Upvotes: 0

Views: 115

Answers (1)

Alessandro Rota
Alessandro Rota

Reputation: 3570

I have created two vertices of class Table2

enter image description here

I have used this code

var g=orient.getGraph();
var b=g.command("sql","select from table2");
for(i=0;i<b.length;i++){
    var record2=b[i];
    var column1=record2.getProperty("column1");
    var column2=record2.getProperty("column2"); 
    var id=record2.getProperty("id"); 
    var record1=g.command("sql","insert into table1 (col1, col2, foreignKey1) values ('"+ column1 + "','" + column2 + "',"+ id + ") return @this");
    g.commit();
    g.command("sql","create edge e from " + record1.getId() + " to " + record2.getId());
}

and I've obtained

enter code here

Hope it helps

Upvotes: 2

Related Questions