First Check
Commit to Help
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
First Check
Commit to Help
Example Code
Description
Instead of using
SQLModel.metadata.create_all(engine), it would be good to selectively add models.Wanted Solution
Why is it useful?
create_allis great for initial setup, but it poses some problems:SQLModelit should use some models but not others. The choice is never really explicit.Suggestion
It might be good to add a
createmethod besides thecreate_allmethod. 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.I don't think so. It sure is handy. But as pointed out, it has its pitfalls too. Whether one uses the
create_allorcreatemethod 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
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