3

I have been trying to use the merge method to create a node using the Py2Neo driver but am having issues.

I try tx.merge(a,"Person",('name','age')) but get the error: TypeError: tuple

In the merge documentation: here it says "Note that multiple property keys may be specified by using a tuple." Am I missing something simple?

John
  • 118
  • 3
  • 11

4 Answers4

3

I've been having the same issue lately, and after reading through the source code provided in the documentation, I've come to the conclusion that the py2neo is wrong with it says that "multiple keys may be specified by using a tuple" as no matter which merge() you call (mostly because they're almost the same, except Graph.merge uses Transaction's autocommit value) it only allows 1 key and doesn't like the tuple type.

As an alternative, you can use a py2neo function that directly executes a Cypher MERGE query to include whatever nodes you're trying to create/merge (ex. Graph.run("MERGE (:Node {...})")). Unfortunately this doesn't really solve the issue, but this might not be in our hands.

  • 1
    If this is an error in py2neo's docs or implementation, have you considered raising an issue on [py2neo's GitHub repo](https://github.com/technige/py2neo)? – WhoIsJack Jul 03 '18 at 23:22
  • 1
    Looks like merging by multiple keys is no longer supported: [this issue](https://github.com/technige/py2neo/issues/675) – Paweł Feb 04 '20 at 11:39
1

I had similar problem in different context; I solved with..

a = Node("Person", name, age)
a.__primarylabel__ = "Person"
a.__primarykey__ = "name"
#a.__primarykey__ = "age"
tx.merge(a) # used graph.merge(a)
Abhijit
  • 11
  • 1
0

Might be an issue with the arguments to the merge function. Perhaps try:

tx.merge(a, primary_label='Person', primary_key=('name', 'age'))

Also note there are two different methods for merge functions in the documentation. See the difference between these two links:

http://py2neo.org/v4/database.html#py2neo.database.Graph.merge http://py2neo.org/v4/database.html#py2neo.database.Transaction.merge

ScottMcC
  • 4,094
  • 1
  • 27
  • 35
  • 1
    Tried that too. Same error. And i've tried both graph.merge() and tx.merge() and get the same error. I've also tried tx.merge(a) and get the error: ValueError: Primary label and primary key are required for MERGE operation. Not sure what else to try – John Jun 28 '18 at 03:30
  • Perhaps try creating a node first before passing it to the merge function. See the following example: https://github.com/technige/py2neo/issues/594 – ScottMcC Jun 28 '18 at 03:48
  • Thanks for the help. I have this `d = {'name': 'foo', 'age': 'bar'}` `a = Node("Person", **d)` `tx.merge(a)` This gives me error: ValueError: Primary label and primary key are required for MERGE operation – John Jun 28 '18 at 03:53
0

Looks like it doesn't work with multiple keys but just one works like this:

        topic = Node("Topic", 
            cname=cname, 
            name=name
        )

        graph.merge(
            topic, "Topic", "name"
        )

where name is the key to merge on.

v4 docs https://py2neo.org/v4/database.html?highlight=merge#py2neo.database.Graph.merge

dcsan
  • 11,333
  • 15
  • 77
  • 118