Skip to content

About automatically adding the id field as the primary key. #123

@vvanglro

Description

@vvanglro

If the user creates the model with an id for the primary key name then everything works fine.
Using a different name will result in the following error.

There is also the case that if I don't declare a primary key field, saffier automatically adds the id field as the primary key, which can be confusing for users. Perhaps for this case you could RAISE an error to remind the user to add the primary key, rather than helping the user to do it implicitly.

class User(saffier.Model):
    ids = saffier.IntegerField(primary_key=True)
    is_active = saffier.BooleanField(default=False)
    created = saffier.DateTimeField(default=datetime.now, server_default=func.now())
    updated = saffier.DateTimeField(default=datetime.now, server_default=func.now(), auto_now=True)

    class Meta:
        registry = models
  File "/Users/hulk/miniforge3/envs/py38/lib/python3.8/site-packages/saffier/core/db/models/metaclasses.py", line 251, in __new__
    raise ImproperlyConfigured(
saffier.exceptions.ImproperlyConfigured: Cannot create model User with multiple primary keys.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions