tarsil / saffier

The only ORM that you will ever need for python.
https://saffier.tarsild.io
MIT License
59 stars 4 forks source link

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

Open vvanglro opened 9 months ago

vvanglro commented 9 months ago

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 commented 9 months ago

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