Use of batch create_or_update raises Exception

72 Views Asked by At

Use of batch create_or_update for a bunch of StructuredNode Objects, raises exception. I created a datamodel in a separate py file for this

Exception occurs:

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/lib/python3.11/site-packages/neomodel/properties.py", line 92, in deflate if properties.get(name) is not None: ^^^^^^^^^^^^^^ AttributeError: 'list' object has no attribute 'get'

Datamodel :

    from neomodel import StructuredNode, StringProperty, RelationshipTo, RelationshipFrom,               StructuredRel, DateProperty

    class Dog(StructuredNode):
    name = StringProperty(required=True)
    owner = RelationshipTo('Person', 'owner')

    class Person(StructuredNode):
    name = StringProperty(unique_index=True)
    pets = RelationshipFrom('Dog', 'owner')

Code

    people = Person.create_or_update(
        {'name': 'Tim', 'age': 83},
        {'name': 'Bob', 'age': 23},
        {'name': 'Jill', 'age': 34},
    )

    listPersons = []
    listPersons.append(Person(name= 'Susan'))
    listPersons.append(Person(name='Bob'))
    Person.create_or_update(listPersons)

Does anyone knows what I'm doing wrong?

1

There are 1 best solutions below

0
Vahid On

The error you're encountering is because create_or_update method in Neomodel expects dictionaries (e.g., keyword arguments) for creating or updating nodes, but you're passing a list of Person objects instead.

To batch create or update StructuredNode objects using create_or_update, you should pass dictionaries containing the property values for each node you want to create or update.

Here's how you can modify your code to achieve this:

people = [
    {'name': 'Tim', 'age': 83},
    {'name': 'Bob', 'age': 23},
    {'name': 'Jill', 'age': 34},
]

Person.create_or_update(*people)

In the code above, we create a list of dictionaries (one for each person) and then pass that list as arguments to Person.create_or_update. The *people syntax is used to unpack the list into separate arguments.

Additionally, if you want to create or update individual Person objects and not use batch processing, you can do it like this:


listPersons = [
    {'name': 'Susan'},
    {'name': 'Bob'},
]

for person_data in listPersons:
    Person.create_or_update(**person_data)

In this case, we iterate through the list of dictionaries and use **person_data to pass each dictionary as keyword arguments to Person.create_or_update.