sqlmodel
sqlmodel copied to clipboard
Choose which model to consider
First Check
- [X] I added a very descriptive title to this issue.
- [X] I used the GitHub search to find a similar issue and didn't find it.
- [X] I searched the SQLModel documentation, with the integrated search.
- [X] I already searched in Google "How to X in SQLModel" and didn't find any information.
- [X] I already read and followed all the tutorial in the docs and didn't find an answer.
- [X] I already checked if it is not related to SQLModel but to Pydantic.
- [X] I already checked if it is not related to SQLModel but to SQLAlchemy.
Commit to Help
- [X] I commit to help with one of those options 👆
Example Code
from typing import Optional
from sqlmodel import Field, SQLModel, create_engine
class Hero(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
name: str
secret_name: str
age: Optional[int] = None
sqlite_file_name = "database.db"
sqlite_url = f"sqlite:///{sqlite_file_name}"
engine = create_engine(sqlite_url, echo=True)
SQLModel.metadata.create(engine, {Hero})
Description
Instead of using SQLModel.metadata.create_all(engine)
, it would be good to selectively add models.
Wanted Solution
Why is it useful?
create_all
is great for initial setup, but it poses some problems:
- Errors when models aren't imported: one can easily forget to import the model, since at no time is it used in this method. One has to remember that these classes are only interpreted as models if they have been imported. This leads to errors (as is pointed out in the documentation). Order matters and that's not implicit.
- Difficulty in setting up a conditional set: when one has a plugin system, for instance, allowing to replace some models with others, it can be tricky to explain to
SQLModel
it should use some models but not others. The choice is never really explicit.
Suggestion
It might be good to add a create
method besides the create_all
method. This method would require the engine and a set of models (as classes). This way, choosing which model to include in the engine would be straightforward enough.
Does
create_all
has to go?
I don't think so. It sure is handy. But as pointed out, it has its pitfalls too. Whether one uses the create_all
or create
method is obviously a personal preference. I personally like the idea of explicitly forwarding the classes to add, if anything, it makes it explicit what models to consider in the engine and makes forgetting to import them impossible.
Thank you for this tool!
Wanted Code
SQLModel.metadata.create(engine, {Hero})
Alternatives
Obviously, both the method name and the parameter (a set) could be adjusted, but the principle would be the same.
Operating System
Windows
Operating System Details
No response
SQLModel Version
0.0.6
Python Version
3.8.10
Additional Context
No response
@vincent-lg you can still use SQLAlchemy API with SQLModel. So you could do
SQLModel.metadata.tables["hero"].create(engine)
# or
Hero.__table__.create(engine)