Skip to content
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

OGM save RelatedObjects - feature flag to keep relationships not in list of nodes #896

Open
process0 opened this issue May 8, 2021 · 4 comments

Comments

@process0
Copy link

process0 commented May 8, 2021

The __db_push__ function in RelatedObjects class will delete any existing relationships for a node that are not represented by the current set of nodes on push. This creates orphan nodes when adding relations via merge to an existing node with existing relationships.

I've commented out line 340 / step 2a in this function and the relationships are preserved. Would it be possible to make this a feature flag? Also, are there any problems that could arise from doing this?

@process0
Copy link
Author

process0 commented May 10, 2021

This is possible by adding a field to the Model class.

--- venv/lib/python3.7/site-packages/py2neo/ogm/__init__.py     2021-05-10 15:52:20.237786016 -0400
+++ other/venv/lib/python3.7/site-packages/py2neo/ogm/__init__.py      2021-05-10 15:47:16.313949017 -0400
@@ -336,8 +337,10 @@
             tx.merge(related_object)
         # 2a. remove any relationships not in list of nodes
         subject_id = self.node.identity
-        tx.run("MATCH %s WHERE id(a) = $x AND NOT id(b) IN $y DELETE _" % self.__relationship_pattern,
-               x=subject_id, y=[obj.__node__.identity for obj, _ in related_objects])
+        if not self.subject.__preserve_relationships__:
+            tx.run("MATCH %s WHERE id(a) = $x AND NOT id(b) IN $y DELETE _" % self.__relationship_pattern,
+                   x=subject_id, y=[obj.__node__.identity for obj, _ in related_objects])
+
         # 2b. merge all relationships
         for related_object, properties in related_objects:
             tx.run("MATCH (a) WHERE id(a) = $x MATCH (b) WHERE id(b) = $y "
@@ -424,6 +427,7 @@
     __primarylabel__ = None
     __primarykey__ = None
 
+    __preserve_relationships__ = False  # False for existing behavior
     __ogm = None
 
     def __eq__(self, other):

@technige
Copy link
Collaborator

technige commented Jun 1, 2021

This sounds like a good feature request. I'll look into it.

@process0
Copy link
Author

@technige any update?

@technige
Copy link
Collaborator

No, not had time to look into this yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants