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

load_node_with_all_properties fails for model with disk property without unique fields or id #243

Open
katarinasupe opened this issue May 5, 2023 · 0 comments
Labels
bug bug community community Effort - Medium Effort - Medium Frequency - Monthly Frequency - Monthly Priority - Later Priority - Later Reach - VeryFew Reach - VeryFew Severity - S3 Severity - S3

Comments

@katarinasupe
Copy link
Contributor

Community report - Discord

When you create a Model with a disk property without unique fields or id it fails to load the node from load_node_with_all_properties. It does not seem to ignore the disk properties and they get added to the where clause.

Minimum reproducible example:

class TestNode(Node):
    #id: int = Field(1, unique=True, db=db)

    Table : int = Field(0, db=db)
    Table2 : int = Field(0, on_disk=True)

checksum = TestNode(Table2=17235).save(db)
checksum_db = TestNode(Table2=17235).load(db)
print(checksum_db)
Saves to sqlite but fails to load back with: Traceback (most recent call last):
  File "/home/ubuntu/mix-alchemy/test.py", line 105, in <module>
    checksum_db = TestNode(Table2=17235).load(db)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/gqlalchemy/models.py", line 620, in load
    node = db.load_node(self)
  File "/home/ubuntu/mix-alchemy/test.py", line 89, in load_node
    result = self.load_node_with_all_properties(node)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/gqlalchemy/vendors/database_client.py", line 224, in load_node_with_all_properties
    return self.get_variable_assume_one(results, "node") 
Back then, I got around the error by inheriting the Node class and overriding the method 
class NodeT(Node):

    #patching to fix on on_disk
    def _get_cypher_field_assignment_block(self, variable_name: str, operator: str) -> str:
            """Creates a cypher field assignment block joined using the operator
            argument.
            Example:
                self = {"name": "John", "age": 34}
                variable_name = "user"
                operator = " AND "

                returns:
                    "user.name = 'John' AND user.age = 34"
            """

            cypher_fields = []
            for field in self.fields:
                value = getattr(self, field)
                attributes = self.fields[field].field_info.extra
                if value is not None and not attributes.get("on_disk", False):
                    cypher_fields.append(f"{variable_name}.{field} = {self.escape_value(value)}")

            return " " + operator.join(cypher_fields) + " "

    def loads(self, db: "Database") -> List["Node"]:  # noqa F821

        node = db.load_node(self)
        for field in self.fields:
            setattr(self, field, getattr(node, field))
        self._id = node._id
        return self

Here's the fix - just follows what is used on the other methods:

def _get_cypher_field_assignment_block(self, variable_name: str, operator: str) -> str:
            """Creates a cypher field assignment block joined using the operator
            argument.
            Example:
                self = {"name": "John", "age": 34}
                variable_name = "user"
                operator = " AND "

                returns:
                    "user.name = 'John' AND user.age = 34"
            """

            cypher_fields = []
            for field in self.fields:
                value = getattr(self, field)
                attributes = self.fields[field].field_info.extra
                if value is not None and not attributes.get("on_disk", False):
                    cypher_fields.append(f"{variable_name}.{field} = {self.escape_value(value)}")

            return " " + operator.join(cypher_fields) + " "
@katarinasupe katarinasupe added the type: bug Something isn't working label May 5, 2023
@katarinasupe katarinasupe added this to the v1.5.0 milestone May 23, 2023
@katarinasupe katarinasupe moved this to Todo in gqlalchemy May 23, 2023
@katarinasupe katarinasupe removed this from the v1.5.0 milestone Sep 15, 2023
@katarinasupe katarinasupe added bug bug Importance - I3 Importance - I3 Severity - S3 Severity - S3 Effort - Medium Effort - Medium community community and removed type: bug Something isn't working labels Dec 28, 2023
@hal-eisen-MG hal-eisen-MG added Priority - Later Priority - Later and removed Priority - Later Priority - Later labels Feb 4, 2024
@hal-eisen-MG hal-eisen-MG added Priority - Later Priority - Later and removed Priority - Later Priority - Later labels Feb 18, 2024
@katarinasupe katarinasupe added Frequency - Monthly Frequency - Monthly Reach - VeryFew Reach - VeryFew and removed Importance - I3 Importance - I3 labels Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug bug community community Effort - Medium Effort - Medium Frequency - Monthly Frequency - Monthly Priority - Later Priority - Later Reach - VeryFew Reach - VeryFew Severity - S3 Severity - S3
Projects
Status: Todo
Development

No branches or pull requests

2 participants