Neo4j--py2neo-- Unable to create nodes using tx.create(a) - neo4j

I am using Py2neo 3.0 and Neo4j 3.0 to create nodes. Followed the transaction statements to create the nodes but failed.
Syntax:
tx = graph.begin()
a= Node("Person1", name="Alicedemo")
tx.create(a)
tx.commit
And, then did the same without transaction, and succeeded.
Syntax:
a= Node("Person1", name="Alicedemo")
graph.create(a)
Is their any problem with transaction in py2neo or else I am missing anything there?

I believe you forgot to use parenthesis
tx.commit()

Related

py2neo - Ne04j write instance

I am using the below code in Python to try connect to Neo4j. However, when I run the code I get the error: "py2neo.database.status.Forbidden: No write operations are allowed on this database. This is a read only Neo4j instance.".
Does anyone know how I can create a write instance to Neo4j from py2neo?
import py2neo
from py2neo import Node, Relationship
g = py2neo.Graph('bolt://neo4j.het.io:7687', bolt=True)
tx = g.begin()
a = Node("Person", name="Alice")
tx.create(a)
b = Node("Person", name="Bob")
ab = Relationship(a, "KNOWS", b)
tx.create(ab)
tx.commit()
g.exists(ab)
neo4j.het.io:7687 is a public, read only Neo4j instance.
Go to https://neo4j.com/download/ and download your own database, and change your script to hit localhost:7687 instead.
(If you want that specific dataset, it is available for free -> https://github.com/dhimmel/hetionet)

Neo4j / Cypher: Is CREATE UNIQUE deprecated?

When I write a simple Cypher query like this:
MATCH (r:Person {name:'Jon'})
MATCH (s:Person {name:'Ana'})
CREATE UNIQUE (r)-[:FRIEND_OF]->(s)
I'm receiving an alert messsage in the Neo4j browser. The alert message says:
The RULE planner is not available in the current CYPHER version, the
query has been run by an older CYPHER version. CREATE UNIQUE is
unsupported for current CYPHER version, the query has been execute by
an older CYPHER version
Here a print screen of the alert message:
I searched by this message in the Neo4j Github and did not find anything. Also the docs has no mention to any depreciation.
My question is: Is CREATE UNIQUE deprecated? Why?
I'm using Neo4j 3.2.1.
Thanks.
PS: I know my query can be refactoring. It is only an example. Also all refactoring made in the query using CREATE UNINQUE show the same alert message in the Neo4j browser.
CREATE UNIQUE is set to be completely replaced by MERGE. So your syntax would be :
MATCH (r:Person {name:'Jon'})
MATCH (s:Person {name:'Ana'})
MERGE (r)-[:FRIEND_OF]->(s)
Regards,
Tom
Try this
MATCH (lft:Person {name:'Jon'}),(rgt)
WHERE rgt.name IN ['Ana']
CREATE UNIQUE (lft)-[r:KNOWS]->(rgt)
RETURN r
note that you can search for multiple names too like this
MATCH (lft:Person {name:'Jon'}),(rgt)
WHERE rgt.name IN ['Ana','Maria']
CREATE UNIQUE (lft)-[r:KNOWS]->(rgt)
RETURN r

neo4j -changing node properties

I'm trying to make a social network and its my first web experience.
I'm using Neo4j database and py2neo module.
Now I want to find a node from my database and change some of it's properties.
I'm using the code below,and i can run it with no errors .but it doesn't change anything in my database and i have no idea why...
please help me if you can.
from py2neo import Graph
graph=Graph()
def edit_name(Uname,name):
person=graph.merge_one("Person","username",Uname)
person.cast(fname=name)
Cast is for casting general Python objects to py2neo objects. For example, if you wanted to cast a Python dictionary to a py2neo Node object, you'd do:
from py2neo import Graph, Node
graph = Graph()
d = {'name':'Nicole', 'age':24}
nicole = Node.cast('Person', d)
However, you still need to pass nicole to Graph.create to actually create the node in the database:
graph.create(nicole)
Then, if you later retrieve this node from the database with Graph.merge_one and want to update properties:
nicole = graph.merge_one('Person', 'name', 'Nicole')
nicole['hair'] = 'blonde'
Then you need to push those changes to the graph; cast is inappropriate for updating properties on something that is already a py2neo Node object:
nicole.push()
TL;DR:
from py2neo import Graph
graph = Graph()
def edit_username(old_name, new_name):
person = graph.merge_one('Person', 'username', old_name)
person['username'] = new_name
person.push()
merge_one will either return a matching node, or, if no matching node exists, create and return a new one. So, in your case, a matching node probably already exists.

this statement is an atomic operation?

I'm new to Neo4j - just started playing with it yesterday.
I have a question - is this statement an atomic operation?
start n = node(68362), n1 = node(68363) match n-[r]->n1 delete r;
YES. If you haven't started the transaction explicitly then Neo4j Server will start one and execute this statement and complete the transaction.
i believe you mean whether neo4j locks any data when executing this delete statement.
than the answer is NO.

Neo4j Cypher: How to stop duplicate SET if multiple CREATES

I have a complex cypher query that creates multiple nodes and increments some counters on those nodes. For sake of example here is a simplified version of what I am trying to do:
START a = node(1), e = node(2)
CREATE a-[r1]->(b {})-[r2]->(c {}), e-[r3]->b-[r4]->(d{})
SET a.first=a.first+1, e.second=e.second+1
RETURN b
The issue is that because there are two CREATE commands the SET commands run twice and the values are incremented by 2 instead of 1 as intended. I have looked to see if I can merge the multiple CREATE statements and I cannot.
My initial idea is to separate out the different creates into a batch query, however I was wondering if there is another option.
Where are you executing this query? What version of neo4j are you using?
I went to console.neo4j.org and successfully ran the following and it correctly added one to both a.first and e.second:
START a = node(1), e = node(2)
CREATE a-[r:KNOWS]->b-[r2:KNOWS]->c, e-[:KNOWS]->b-[:KNOWS]->d
SET a.first=a.first+1, e.second=e.second+1
RETURN b

Resources