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

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

Open
vvanglro opened this issue Jan 15, 2024 · 1 comment · May be fixed by #127
Open

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

vvanglro opened this issue Jan 15, 2024 · 1 comment · May be fixed by #127
Labels
enhancement New feature or request

Comments

@vvanglro
Copy link
Contributor

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.
@tarsil
Copy link
Owner

tarsil commented Jan 15, 2024

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.

Great idea actually. Thank you for this @vvanglro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
2 participants