Replies: 4 comments
|
I can confirm. Here is a simplified version for testing (compatible with Python 3.7+): from typing import List, Optional
from sqlmodel import Field, SQLModel, create_engine, Session, Relationship
class Foo(SQLModel, table=True):
id: Optional[int] = Field(primary_key=True)
bars: List["Bar"] = Relationship(back_populates="foo")
class Bar(SQLModel, table=True):
__mapper_args__ = {
"polymorphic_on": "type",
"polymorphic_identity": "x",
}
id: Optional[int] = Field(primary_key=True)
type: str = Field(default="x")
foo_id: Optional[int] = Field(foreign_key="foo.id")
foo: Optional[Foo] = Relationship(back_populates="bars")
class BarY(Bar, table=True):
__mapper_args__ = {
"polymorphic_identity": "y",
}
class BarZ(Bar, table=True):
__mapper_args__ = {
"polymorphic_identity": "z",
}
db_url = f"sqlite:///:memory:"
engine = create_engine(db_url, echo=True)
SQLModel.metadata.create_all(engine)
with Session(engine) as session:
foo = Foo()
foo.bars.extend([BarY(), Bar()])
session.add(foo)
session.commit()The full error: (Click to expand)Here is an analogous version in pure SQLAlchemy working with no errors: (Click to expand)from sqlalchemy.engine.create import create_engine
from sqlalchemy.orm import relationship
from sqlalchemy.orm.decl_api import declarative_base
from sqlalchemy.sql.schema import Column, ForeignKey
from sqlalchemy.orm.session import Session
from sqlalchemy.sql.sqltypes import Integer, String
Base = declarative_base()
class Foo(Base):
__tablename__ = 'foo'
id = Column(Integer, primary_key=True)
bars = relationship("Bar", back_populates="foo")
class Bar(Base):
__tablename__ = 'bar'
__mapper_args__ = {
"polymorphic_on": "type",
"polymorphic_identity": "x",
}
id = Column(Integer, primary_key=True)
type = Column(String, default="x")
foo_id = Column(ForeignKey("foo.id"))
foo = relationship("Foo", back_populates="bars")
class BarY(Bar):
__mapper_args__ = {
"polymorphic_identity": "y",
}
class BarZ(Bar):
__mapper_args__ = {
"polymorphic_identity": "z",
}
db_url = f"sqlite:///:memory:"
engine = create_engine(db_url, echo=True)
Base.metadata.create_all(engine)
with Session(engine) as session:
foo = Foo()
foo.bars.extend([BarY(), Bar()])
session.add(foo)
session.commit()Interestingly, you get a different error, if you instead just try to instantiate a ...
with Session(engine) as session:
bar = BarY()
session.add(bar)
session.commit()The full error: (Click to expand)I tried debugging the new_cls = super().__new__(cls, name, bases, dict_used, **config_kwargs)When setting up So that seems not to be the issue. This is all I got so far. |
|
Someone helped me out with a slightly modified version that works for me: |
|
I tried this workaround with With "inherit_condition" SQLAlchemy maked the correct join but thinks all columns are in the child table. There also a warning on start up, "SAWarning: Implicitly combining column parent.id with column child.id under attribute 'id'. Please configure one or more attributes for these same-named columns explicitly." Any idea how to make this work? |
|
Hello~, the current version is sqlmodel 0.0.19. Does anyone have idea about how to solve this? |
Uh oh!
There was an error while loading. Please reload this page.
First Check
Commit to Help
Example Code
Description
I have this PoC-style code that would model a 1:n relationship between one
InvoiceRequestand multipleInvoices.There are several invoice types
ÌnvoiceReversalandInvoiceCorrectionthat are modelled using the inheritance fromInvoiceclass and polymorphic identity.However. the derived classes are obviously not mapped:
sqlalchemy.orm.exc.UnmappedClassError: Class '__main__.InvoiceReversal' is not mappedI could not find a solution how to resolve this.
Operating System
Linux
Operating System Details
Linux
SQLModel Version
SQLAlchemy==1.4.40
Python Version
3.10
Additional Context
All reactions