When executing the above example code, the output shows that only the enum from FlatModel is correctly created, while the enum from the inherited class is not:
CREATETYPEmyenumAS ENUM ('A', 'B');
# There should be a TYPE def for myenum2, but isn'tCREATETABLEflatmodel (
enum_field myenum,
id UUID NOT NULL,
PRIMARY KEY (id)
);
CREATEINDEXix_flatmodel_idON flatmodel (id);
CREATETABLEinheritmodel (
enum_field myenum2,
id UUID NOT NULL,
PRIMARY KEY (id)
);
CREATEINDEXix_inheritmodel_idON inheritmodel (id);
Well the partial-fix seems to be addressed in a number of PRs (#24 , #165 - which i created not knowing #24 was already out there). There is still an issue with inheritance if you manually use sa_column in the non-table parent class but I didn't get anywhere understanding why this was the case.
Are there more maintainers other than @tiangolo who can look over these PRs and get one of them merged?
Reacted by Toni Alatalo, Jonathan Lifschutz, fraschm1998, Kehan Harman, Daniel Davis, voxofox, chentt, Javier van der Meulen, Tim OSullivan, Ben Rapaport and 1 more
I can confirm that if you do not use sa_column for the field, this is now working as expected with sqlmodel:0.0.8 and sqlalchemy:1.4.41.
However, a word to other who might end up here - it does NOT work if you manually define the field via sa_column:
classBaseModel(SQLModel):
id: uuid.UUID=Field(primary_key=True)
# this will not work#enum_field: MyEnum2 = Field(sa_column=Column(Enum(MyEnum2)))# this will work#enum_field: MyEnum2 = Field()
An addendum - defining the enum with a string mixin (for nicer on-the-wire strings used with fastapi) using the following notation also breaks the type creation DDL (in that It will not create the dedicated type and the field will just be a varchar):
I assume this could easily be fixed by amending the if statement order in main.py:get_sqlalchemy_type function to test the subclass is an Enum before string:
defget_sqlachemy_type(field: ModelField) ->Any:
ifissubclass(field.type_, str):
iffield.field_info.max_length:
returnAutoString(length=field.field_info.max_length)
returnAutoString
...
# move this to the top?ifissubclass(field.type_, Enum):
returnsa_Enum(field.type_)
...
An addendum - defining the enum with a string mixin (for nicer on-the-wire strings used with fastapi) using the following notation also breaks the type creation DDL (in that It will not create the dedicated type and the field will just be a varchar):
classMyEnum(str, enum.Enum):
A='A'B='B'
Any news on this point? I am having this exact issue with enums defined with string mixin (sqlmodel 0.0.6 and 0.0.8 tested without success).
Activity
chriswhite199 commented on Nov 24, 2021
The equiv straight sqlalchemy example code works without issue:
Output
Enum fixes (typing and inheritance)
chriswhite199 commented on Nov 24, 2021
Well the partial-fix seems to be addressed in a number of PRs (#24 , #165 - which i created not knowing #24 was already out there). There is still an issue with inheritance if you manually use sa_column in the non-table parent class but I didn't get anywhere understanding why this was the case.
Are there more maintainers other than @tiangolo who can look over these PRs and get one of them merged?
olafdeleeuw commented on Jun 24, 2022
I'm having the same issue but posted it in the closed issue #164:
tiangolo commented on Aug 27, 2022
Thanks everyone for the discussion. ☕
If you want to help me, the best help I can get is helping others with their questions in issues. That's what consumes most of the time.
Maybe this was solved in #165. The fix will be available in the next release, SQLModel 0.0.7, in the next hours. 🎉
Could you check and confirm?
github-actions commented on Sep 7, 2022
Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.
chriswhite199 commented on Sep 7, 2022
I can confirm that if you do not use sa_column for the field, this is now working as expected with sqlmodel:0.0.8 and sqlalchemy:1.4.41.
However, a word to other who might end up here - it does NOT work if you manually define the field via sa_column:
chriswhite199 commented on Sep 7, 2022
An addendum - defining the enum with a string mixin (for nicer on-the-wire strings used with fastapi) using the following notation also breaks the type creation DDL (in that It will not create the dedicated type and the field will just be a varchar):
chriswhite199 commented on Sep 7, 2022
I assume this could easily be fixed by amending the if statement order in main.py:get_sqlalchemy_type function to test the subclass is an Enum before string:
tepelbaum commented on Nov 29, 2022
Any news on this point? I am having this exact issue with enums defined with string mixin (sqlmodel 0.0.6 and 0.0.8 tested without success).
Will we be good to go when #442 is merged? :)
tiangolo commented on Oct 23, 2023
Thanks @chriswhite199 and @tepelbaum! This was solved in #669
It will be available in the next release, 0.0.9. 🎉
tepelbaum commented on Oct 23, 2023
Thanks @tiangolo , and thanks a lot for this!! Can't wait for the pydantic V2 support :)