-
Notifications
You must be signed in to change notification settings - Fork 232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New function required: StructuredNode.merge #438
Comments
@john-theo On behalf of the ~30 developers who have contributed to
It depends on what are you trying to do but from what you describe, the best thing to do is to use Hope this helps. |
Sorry for not describing it clearly.
The Many thanks in advance. |
@john-theo What is it that you are trying to achieve? Let's not worry about how it is going to be done at the moment. |
Let's say we are going to add a bunch of nodes and relations to a graph (For example, multiple articles and authors), and authors who previously wrote other articles are already in the graph. Assuming the unique index of Now, I have many new articles and authors to be inserted into the graph. The plain neo4j CREATE CONSTRAINT ON (author:Author) ASSERT author.full_name IS UNIQUE
CREATE (b:Author {full_name: 'maybe a new author'})-[r:WROTE]->(a:Article {title: 'new article'})
RETURN a,r,b The class Author:
full_name = StringProperty(unique_index=True, required=True)
article = Article(title="new article").save()
author = Author(full_name="maybe a new author").save()
author.wrote.connect(article) And these expressions may trigger a ConstraintError if an To deal with this situation, a better(-ish) way to carry out the node insertion would be: The plain neo4j MERGE (b:Author {full_name: 'maybe a new author'})-[r:WROTE]->(a:Article {title: 'new article'})
RETURN a,r,b I wanted to perform a similar action in Yes, I can first query whether the The function I contributed can be used like this: author = Author(full_name="maybe a new author").merge() In my own project, I bind the function with the Um, that's pretty much the whole story, and thanks for your time and patience. |
As someone who has created a platform to process scientific literature data at the scale of a few GB (with a single desktop) and above (with distributed queries over the enterprise version of Neo4J), all based on
Now, having said this:
You have identified this part (and the text leading up to it of course) correctly. Indeed, The way to do this (that respects indices) is (In fact, when you do this, you might still come across another "bug" that is related to this pull request where you have to establish a nested arrangement of A typical approach here would be to first create all the nodes and then all connections between them. For more information please see this issue here and the ensuing discussion.
This is done for a reason. Some Have a look at Hope this helps. |
Now it's crystal clear! Thank you so much! A guide like this should be in the project document to help neo4j starters like me. I'm closing this issue and the related pull request. Thanks again! |
@john-theo No worries, feel free to reach out anytime. |
I still don't understand why there is no merge operation on single nodes. I work on a project which modifies only a few nodes and relationships in our graph, based on incoming event messages, not knowing which properties are changed. So I have to get every single node, just to check if it's already there, overwrite every single property for it might have changed and then save it again or for the first time. To create an instance and just merge would be much easier for me. |
@P1zz4br0etch3n I am sorry, I did not spot this earlier.
Because it is impossible to "merge" the Every node has an Attempting to merge without having filled in this This is why the node has to first be retrieved. A possible solution might be to use recently added functionality described here. You can do a lazy Hope this helps (?) |
Thanks for your reply. I get your point, but what if you implement the merge() method just the same way like the create_or_update() method: matching by its required and/or unique properties? |
@P1zz4br0etch3n I understand. At the moment I cannot see how would we go around trying to merge a node and not having the |
Maybe it's getting clear if I explain a little bit how our model currently works. We have a property named "key" on every node type, which is kind of our primary key. We force this property to be unique, required and indexed by adding a "is key" constraint on it, like it is explained here. To create or update such a node we use a statement like I didn't look deeper into the implementation of your |
As the case in April, I implemented my own Now I'm working on another project using from neomodel import *
from random import randint
config.DATABASE_URL = 'bolt://neo4j:mypassword@localhost:7687'
config.AUTO_INSTALL_LABELS = True
class Person(StructuredNode):
name = StringProperty(unique_index=True)
age = IntegerProperty(required=True)
john = Person(name='John', age=randint(5,80))
alice = Person(name='Alice', age=randint(30,40))
Person.create_or_update(*[
x.__dict__ for x in [john, alice]
])
Person.create_or_update(*[
x.__dict__ for x in [john, alice]
]) As documented here, it works fine. But if I run the following code TWICE, I got an error. from neomodel import *
from random import randint
config.DATABASE_URL = 'bolt://neo4j:mypassword@localhost:7687'
config.AUTO_INSTALL_LABELS = True
class Person(StructuredNode):
name = StringProperty(unique_index=True)
age = IntegerProperty(required=True)
john = Person(name='John', age=randint(5,80))
alice = Person(name='Alice', age=randint(30,40))
Person.create_or_update(*[
x.__dict__ for x in [john, alice]
]) The error is as follows:
I was intended to batch update Person Node information, is there a proper way to do this? |
Awesome awesome work!
I want to do
a = Article(title='some title').merge()
like actual MERGE action in Cypher.The lines below are from
neomodel.core
. Theif
statement on line 520 decide whether to update or create the node. THE PROBLEM IS: How should the node has the attribute ofid
when it was just created?OR, is there already a way to do something like
a = Article(title='some title').merge()
? I went through the documentation and the codes, foundget_or_create
andcreate_or_update
, but they are not what I want.Many thanks!
neomodel/neomodel/core.py
Lines 512 to 534 in cca5de4
The text was updated successfully, but these errors were encountered: