saffier icon indicating copy to clipboard operation
saffier copied to clipboard

About automatically adding the id field as the primary key.

Open vvanglro opened this issue 1 year ago • 1 comments

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.

vvanglro avatar Jan 15 '24 03:01 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.

Great idea actually. Thank you for this @vvanglro

tarsil avatar Jan 15 '24 06:01 tarsil