-
-
Notifications
You must be signed in to change notification settings - Fork 395
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
feat: add mixin for int enum field custom #1781
base: develop
Are you sure you want to change the base?
feat: add mixin for int enum field custom #1781
Conversation
725ff99
to
46f2bb1
Compare
Pull Request Test Coverage Report for Build 12028090078Details
💛 - Coveralls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about making IntEnumFieldInstance
inheriting the regular IntField
which will extend the potential values up to 2147483647? This should be enough for 99.9999% cases. If someone needs more, they can create a custom field.
I think we want to keep our Fields simple.
46f2bb1
to
f0884f4
Compare
…auto-choose-field-type-for-IntEnumField
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still think we need to make IntEnumField
an integer field (which is going to require a migration for users unfortunately but this would be a correct way to do it), not a small integer field. This will cover 99.9% cases and we won't have to complicate the code and making extra documentation for a very rare edge case. For the people who need big integers, they can just copy the related code from data.py
and make their own field.
class IntEnumFieldInstance(IntEnumFieldMixin, SmallIntField): | ||
pass | ||
|
||
|
||
def IntEnumField( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really confusing that IntEnumField
actually returns SmallIntField
:(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but mostly, IntEnumField used in the following case:
class Gender(IntEnum):
male = 0
female = 1
other = 2
class Users(Model):
gender = fields.IntEnumField(Gender)
And I wish it to be small int
in db, not int.
I tried this one: -class IntEnumFieldInstance(SmallIntField):
+class IntEnumFieldInstance(IntField): And then run from tortoise import BaseDBAsyncClient
async def upgrade(db: BaseDBAsyncClient) -> str:
return """
ALTER TABLE "users" ALTER COLUMN "gender" TYPE INT USING "gender"::INT;"""
async def downgrade(db: BaseDBAsyncClient) -> str:
return """
ALTER TABLE "users" ALTER COLUMN "gender" TYPE SMALLINT USING "gender"::SMALLINT;"""
|
I am more inclined to have default enum as smallint, as it is indeed covers most sane cases of using enums Although maybe we should document it somehow better |
Let's see how other ORMs implement this! DjangoIn Django you choose the field, e.g. SQLAlchemySQLAlchemy "will make use of the backend’s native “ENUM” type if one is available; otherwise, it uses a VARCHAR datatype". Native ENUMs are great but Django's approach is more universal and supported by more databases and I like it more personally. I think we can consider implementing something like that in Tortoise instead of the existing enum approach but not as part of this PR obviously. I agree with Andrey that small int should cover most sane use cases of using enums. I don't like the idea of introducing mixins and making the fields classes and documentation more complicated for resolving some very peculiar use case. I think we can just update the docstring of |
Description
As #966 said, IntEnumField inherit from SmallIntField, so it can not use a enum_type with value > 32767
Motivation and Context
This PR add a IntEnumFieldMixin to make it easy to custom a new IntEnumField:
How Has This Been Tested?
make ci
Checklist: