Open
Description
First Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn't find it.
- I searched the SQLModel documentation, with the integrated search.
- I already searched in Google "How to X in SQLModel" and didn't find any information.
- I already read and followed all the tutorial in the docs and didn't find an answer.
- I already checked if it is not related to SQLModel but to Pydantic.
- I already checked if it is not related to SQLModel but to SQLAlchemy.
Commit to Help
- I commit to help with one of those options 👆
Example Code
from sqlmodel import Relationship, SQLModel, Field, create_engine
from typing import Optional
import uuid
class Principal(SQLModel, table=True):
__tablename__ = "users"
id: Optional[uuid.UUID] = Field(primary_key=True,nullable=False,default_factory=uuid.uuid4)
is_active:bool = Field(default=True)
type: str = Field(default="principal")
__mapper_args__ = {
'polymorphic_on':'type',
'polymorphic_identity':'principal'
}
class User(Principal,table=True):
email: str
__mapper_args__ = {
'polymorphic_identity':'user'
}
class ServiceUser(Principal,table=True):
name: str
owner_id: Optional[uuid.UUID] = Field(default=None, foreign_key=('users.id'))
owner: "User" = Relationship()
__mapper_args__ = {
'polymorphic_identity':'serviceuser'
}
sqlite_file_name = "database.db"
sqlite_url = f"sqlite:///{sqlite_file_name}"
engine = create_engine(sqlite_url, echo=True)
SQLModel.metadata.create_all(engine)
Description
- Run the code above
- Look at the echo from the database:
<excluded for brevity>
2021-08-26 21:21:45,820 INFO sqlalchemy.engine.Engine
CREATE TABLE users (
id CHAR(32) NOT NULL,
is_active BOOLEAN,
type VARCHAR,
PRIMARY KEY (id)
)
<excluded for brevity>
- Sit back and wonder why the inherited objects fields are not included (notice the create table statement not including the fields)
Operating System
Windows
Operating System Details
No response
SQLModel Version
0.0.4
Python Version
3.8.10
Additional Context
I think I can fall back to sqlalchemy in this case without any problems, but maybe I am at a loss and it should be done in another way. Removing the "table=True" from the inherited classes makes no difference. Maybe this is also an edge case that should not be supported, but anyway it would be nice to see how this should be handled by people smarter than me. I am currently evaluating rewriting a backend to sqlmodel as it is already implemented in FastApi (which is amazing), and although I know it's early days for this project, I like what it tries to achieve :)