sqlmodel icon indicating copy to clipboard operation
sqlmodel copied to clipboard

:bug: Fix enum type checks ordering in `get_sqlalchemy_type`

Open pierrecdn opened this issue 3 years ago β€’ 5 comments

When enum subclassing/mix-in[1] is used, especially to ensure compatibility and produce correct OpenAPI specifications with FastAPI[2], SQLModel on its side doesn't map to the correct SQLAlchemy type.

e.g.:

  • class Foo(Enum) will work but won't allow a proper OpenAPI spec. to be generated.
  • class Foo(str, Enum) will fix the previous situation, but prevents SQLModel to read and map the object from the DB (due to validation errors).

Changing ordering of checks to ensure that Enum subclasses are tested first.

[1] https://docs.python.org/3/library/enum.html#restricted-enum-subclassing [2] https://fastapi.tiangolo.com/sq/tutorial/path-params/#create-an-enum-class

pierrecdn avatar Sep 08 '22 22:09 pierrecdn

πŸ“ Docs preview for commit a6d9568788b483b61443b563e7878e7b1ec32752 at: https://631a68e284fe1d20404a6714--sqlmodel.netlify.app

github-actions[bot] avatar Sep 08 '22 22:09 github-actions[bot]

Codecov Report

Merging #442 (a6d9568) into main (75ce455) will not change coverage. The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main     #442   +/-   ##
=======================================
  Coverage   97.76%   97.76%           
=======================================
  Files         187      187           
  Lines        6268     6268           
=======================================
  Hits         6128     6128           
  Misses        140      140           
Impacted Files Coverage Ξ”
sqlmodel/main.py 84.92% <100.00%> (ΓΈ)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

codecov[bot] avatar Sep 08 '22 22:09 codecov[bot]

Hmm, seeing that I'm not the only one to observe this:

  • https://github.com/tiangolo/sqlmodel/issues/164#issuecomment-1239909973
  • https://github.com/tiangolo/sqlmodel/pull/366 (with a different approach though)

@tiangolo any thoughts?

pierrecdn avatar Sep 08 '22 22:09 pierrecdn

This makes sense. It may need to be rebased with PR #425.

If PR #436 is merged, you'll be able to specify the SQL type explicitly whenever you want. Then PR #366 will become obsolete. It's still worth maintaining/optimizing get_sqlachemy_type though to cover the simple and straightforward cases, so that you don't need to specify the SQL type.

daniil-berg avatar Sep 09 '22 07:09 daniil-berg

If PR https://github.com/tiangolo/sqlmodel/pull/436 is merged, you'll be able to specify the SQL type explicitly whenever you want. Then PR https://github.com/tiangolo/sqlmodel/pull/366 will become obsolete. It's still worth maintaining/optimizing get_sqlachemy_type though to cover the simple and straightforward cases, so that you don't need to specify the SQL type.

Indeed, just seen you proposal (#436), sounds really nice. The PR is quite large though, so it might take longer to get merged.

pierrecdn avatar Sep 09 '22 07:09 pierrecdn

Thank you!

As I can't push to this branch, I included your commits in a new branch in a new PR here: https://github.com/tiangolo/sqlmodel/pull/669

Given that, I'll close this one. Thanks for the work! πŸ™‡

tiangolo avatar Oct 23 '23 09:10 tiangolo