0

I have a neomodel model:

class User(StructuredNode):
    id = UniqueIdProperty()
    username = StringProperty(unique_index=True)
    email = StringProperty(unique_index=True)
    password = StringProperty()

    def save(self, *args, **kwargs):
        self.password = make_password(self.password)
        return super(User, self).save(*args, **kwargs)

When I save a new instance of such model in the console, I get a new User instance successfully, but when I retrieve all User objects from model, I get an empty resultset:

(AttractoraVenv) MacBook-Pro-de-Hugo:AttractoraBackend hugovillalobos$ python manage.py shell
Python 3.7.1 (default, Dec 14 2018, 13:28:58)
[Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from users.models import User
>>> user = User(username='admin', email='admin@admin.com', password='secret').save()
>>> user
<User: {'id': '0d3b0aa56499414d8fb205ea1279662e', 'username': 'admin', 'email': 'admin@admin.com', 'password': 'pbkdf2_sha2
56$150000$vTdAnqAvXnOM$0hPMc1KiMpLvHbVO4C77OXUa9dmKzt9lMxIg8Ig+za8='}>
>>> User.nodes.all()
[]

I don't know what I am missing.

EDIT

I found that when I create a User instance using cypher from neo4j console, User.nodes.all() returns such instance. So I guess the problem is on User.save(). I also found that User.save() works well when updating, so the problem is on create.

HuLu ViCa
  • 5,077
  • 10
  • 43
  • 93

2 Answers2

2

id is a built-in attribute and it's not a good idea to override it. You can replace id = UniqueIdProperty() with something like userid = UniqueIdProperty() and that should address the issue.

user3280193
  • 450
  • 1
  • 6
  • 13
  • can you link the the docs that say id is a built-in? I just had this problem and it took me a while to find this post. – Evando Jun 08 '20 at 19:18
  • Just stumbled on this answer and it helped me figure out why save() seemed to be failing silently. See https://neomodel.readthedocs.io/en/latest/properties.html?highlight=id#unique-identifiers for more details advising against app use of ID and the UniqueIdProperty alternative – Draineh Dec 29 '22 at 15:35
0

I'm not sure overriding the save method is the good way to go.

Try this instead (remove completely the save method):

class User(StructuredNode):
    id = UniqueIdProperty()
    username = StringProperty(unique_index=True)
    email = StringProperty(unique_index=True)
    password = StringProperty()

    def pre_save(self):
        self.password = make_password(self.password)
Muldec
  • 4,641
  • 1
  • 25
  • 44