Until this gets fixed, if you don't want to set index=False on every single attribute, you can create a decorator on Field function and have it override the index param setting the default to False. This works as long as you make sure to call Field() for each of your model attributes.
defset_default_index(func):
"""Decorator to set default index for SQLModel Can be removed when https://github.com/tiangolo/sqlmodel/pull/11 is merged """@wraps(func)definner(*args, index=False, **kwargs):
returnfunc(*args, index=index, **kwargs)
returninner# monkey patch field with default index=False# this works as long as we always call Field()Field=set_default_index(Field)
Activity
CaselIT commented on Aug 25, 2021
It seems to add an index by default
https://github.com/tiangolo/sqlmodel/blob/af03df88acda8c5fb3e0e755b0ab0517a2c8aabb/sqlmodel/main.py#L419-L420
I think it's just a typo there
falkben commented on Nov 20, 2021
Until this gets fixed, if you don't want to set index=False on every single attribute, you can create a decorator on Field function and have it override the index param setting the default to False. This works as long as you make sure to call Field() for each of your model attributes.
To be used like:
Creates a table with name column with an index but description does not.
Without calling Field() for description, you'd still end up with an index.
tiangolo commented on Dec 28, 2021
Thank you @peterHoburg for bringing it up, thanks @CaselIT for the PR, and thanks @falkben for the workaround! 🚀 🍰
This should be solved in #205, the new docs explaining indexes are here: https://sqlmodel.tiangolo.com/tutorial/indexes/
Indexes are now opt-in and not created by default. You can read more about it all in that PR.
It is now released as part of SQLModel 0.0.6. 🎉
github-actions commented on Jan 8, 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.
Merge pull request fastapi#9 from NEXT-JP/patch-1