sqlmodel
sqlmodel copied to clipboard
π Support mixins
Add support for mixins to be used with SQLModel
. See https://github.com/tiangolo/sqlmodel/issues/254.
I thought I was already using mixins, when say this in our fastapi-users integration (using https://github.com/fastapi-users/fastapi-users-db-sqlmodel)
class User(SQLModelBaseUserDB, UserBase, table=True):
But am guessing that works because both those classes inherit SQLModel.
Just double checking: is it a good idea to add this to support mixins that are not SQLModel's? I guess yes.
Would it be possible for you to test this with your project, @antont? It would be reassuring if your tests pass as well.
Would it be possible for you to test this with your project, @antont? It would be reassuring if your tests pass as well.
Yes, I can do that either later today or else at some point tomorrow.
Is there hope for a merge at all? I see a long list of useful PRs that haven't been merged and are outdated by now.
Is there hope for a merge at all? I see a long list of useful PRs that haven't been merged and are outdated by now.
I just merge the PRs we need in our company fork (public). I guess others do the same. But yes there is hope, tiangolo seems to merge too and release when he has time, is just busy working on these on other fronts too, fastapi, pydantic etc.
SQLModel is quite small and was pretty solid when it was first released, I'd bet most of the PRs are not outdated as not much has changed.
@byrman - works for me, so didn't break mixins that derive from SQLModel. makes sense too, didn't suspect problems either and the change seems logical too.
Thanks for testing in the wild, @antont!
Hope to see this merged soon π
Until this is merged, here's an easy workaround:
class MyMixin():
__config__ = None # <---- THIS!
# your code here
class MyModel(SQLModel, MyMixin, table=True):
# your code here
Unfortunately, this use case is not covered: https://github.com/tiangolo/sqlmodel/issues/330. Only methods and simple attributes appear to work.
Thanks for testing these solutions.
@bolau I just tried your suggested approach and am having an issue where the introduction of __config__ = None
is leading to an AttributeErrorwithin
pydantic`. Is this something you've encountered before?
File "/code/./library/module/utils.py", line 15, in <module>
class ActiveRecord(SQLModel):
File "/usr/local/lib/python3.9/site-packages/sqlmodel/main.py", line 277, in __new__
new_cls = super().__new__(cls, name, bases, dict_used, **config_kwargs)
File "/usr/local/lib/python3.9/site-packages/pydantic/main.py", line 292, in __new__
cls.__try_update_forward_refs__()
File "/usr/local/lib/python3.9/site-packages/pydantic/main.py", line 773, in __try_update_forward_refs__
update_model_forward_refs(cls, cls.__fields__.values(), cls.__config__.json_encoders, {}, (NameError,))
AttributeError: 'NoneType' object has no attribute 'json_encoders'
Codecov Report
Merging #256 (3c68587) into main (75ce455) will increase coverage by
0.00%
. The diff coverage is100.00%
.
@@ Coverage Diff @@
## main #256 +/- ##
=======================================
Coverage 97.76% 97.77%
=======================================
Files 187 188 +1
Lines 6268 6280 +12
=======================================
+ Hits 6128 6140 +12
Misses 140 140
Impacted Files | Coverage Ξ | |
---|---|---|
sqlmodel/main.py | 84.92% <100.00%> (ΓΈ) |
|
tests/test_mixin.py | 100.00% <100.00%> (ΓΈ) |
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.
π Docs preview for commit 3c68587bab8c9ff940263a665173d04c3c3e0ae8 at: https://630f80b819e9472c1e2ab9ba--sqlmodel.netlify.app
Sorry, no. Maybe it's not a good workaround after all ;-) Hope that this gets merged sometime soon...
#256 fixes Mixin usage with https://github.com/absent1706/sqlalchemy-mixins ;)
π Docs preview for commit 6f0c4bb80281716b57b53dbfcfff55565a56d446 at: https://639ce01f2b35ad0062e48357--sqlmodel.netlify.app
Well... up ! Any reason mixin support cannot be merged ?
@byrman will relationship fields in mixins work with this?
@byrman will relationship fields in mixins work with this?
I'm afraid not, take a look at https://github.com/tiangolo/sqlmodel/issues/330.
Until this is merged, here's an easy workaround:
class MyMixin(): __config__ = None # <---- THIS! # your code here class MyModel(SQLModel, MyMixin, table=True): # your code here
This worked well for me. I don't see official support for mixins in the documentation yet.
@tiangolo Sorry to bother, but any reason this actually doesn't get pulled ?
The __config__ = None
workaround works for me, but causes this mypy error (as expected):
error: Definition of "__config__" in base class "BaseModel" is incompatible with definition in base class "MyMixin"