1

I'm now trying to build my first Neo4j app with Django + neo4django. i feel the library is cool, but has several serious problems for now. (i understand, they are in still developing-version now.) Is there a way to update an exists node (or modify property-value)?

neo4django - User Documentation — neo4django 0.1.7-dev documentation https://neo4django.readthedocs.org/en/latest/index.html

Matt Luongo
  • 14,371
  • 6
  • 53
  • 64

1 Answers1

0

Of course- the same way you would with the Django ORM.

If you have a model, Person, like this

class Person(models.NodeModel):
    name = models.StringProperty()

pete = Person.objects.create(name='Pete')

You can simply update the model instance attribute, and save

pete.name = 'Peter'
pete.save()

Do you think more links to the Django docs, or maybe an example project, would make this more clear in the documentation? Or maybe a more information about properties in the "Writing Models" section?

EDIT - from new information in the comments.

The error you're referencing (ValueError: Duplicate index entries for <Model>.prop) is because you're trying to save a model property that's been marked as "unique", with a value that's already been used. The unique=True option makes sure to check the type index first, and throws an error if the value has already been used. It's expected behavior.

Consider

class UniquePerson(models.NodeModel):
     name = models.StringProperty(indexed=True, unique=True)

>>> pete = Person.objects.create(name='Pete')
>>> peter = Person.objects.create(name='Pete')
...
ValueError: Duplicate index entries for <UniquePerson>.name
>>> pete.name = 'other pete'
>>> pete.save()
>>> peter = Person.objects.create(name='Pete')
>>> #no problem, since the original pete node now has a different name

If you don't want that behavior, you can of course flip off unique=True, catch the error, or check whether an object with that property already exists like pete = Person.objects.get(name='Pete').

EDIT - 4/3/13 - Found a contributing bug.

A couple days ago, I found a bug in neo4django that might've led to what you're seeing. It kept nodes with a unique=True property from being saved after they'd already been saved to the database, and made it impossible to update nodes with a property like that.

I opened an issue, made sure the test suite catches it, and provided a patch - https://github.com/scholrly/neo4django/issues/150- hopefully that solves your problem!

To get the latest source from GitHub, you can use pip install -e git+https://github.com/scholrly/neo4django#egg=neo4django-dev. Let me know if that fixes it.

Matt Luongo
  • 14,371
  • 6
  • 53
  • 64
  • Thank you for the answering. Of course I tried with "Writing Models" section at the first,and it works fine. But in the case the property has 'unique=True' option, save method doesn't work well.(raised "Duplicate index entries".) Please tell me the rihgt way. – Hideki Kinjyo Mar 24 '13 at 06:18
  • @HidekiKinjyo if you could update your question with those details, that would be really helpful! In fact, a code snippet illustrating your problem could help as well. – Matt Luongo Mar 24 '13 at 20:07
  • @HidekiKinjyo did this solve your problem? And if so, could you accept the answer? If not- what additional problems are you running into? – Matt Luongo Mar 27 '13 at 13:30
  • @HidekiKinjyo I think I found (and squashed) the bug that led to your problem- please let me know! – Matt Luongo Apr 03 '13 at 22:59