Relationship problem "When initializing mapper" #900
-
First Check
Commit to Help
Example Codeclass BaseEntity(SQLModel):
"""Class with base data from the entities"""
id: int | None = Field(default=None, primary_key=True)
data_creation: str
data_udpate: str | None
class Config:
arbitrary_types_allowed = True
class Product(
BaseEntity,
table=True,
):
"""Class the products"""
__tablename__ = "products"
name: str = Field(default="", le=200)
description: TEXT = Field(default="", sa_type=TEXT)
product_code: str = Field(default="")
production_country: str = Field(default="", index=True)
production_city: str = Field(default="", index=True)
csr_code: int = Field(default_factory=lambda: randint(100000, 999999))
csr_image: LONGBLOB = Field(default=None,nullable=False, sa_type=LONGBLOB)
user_id: int = Field(default=None, foreign_key="users.id")
user: "User" = Relationship(back_populates="products")
class User(
BaseEntity,
table=True
):
"""Class the users"""
__tablename__ = "users"
name: str = Field(default="", le=200)
email: str = Field(default="", le=200, index=True, unique=True)
password: str = Field(le=500)
products: list["Product"] = Relationship(back_populates="user") Descriptionat the frist query i get this message. but only if i add the navigation properties, if i use only the id to create the relation, all works Operating SystemmacOS Operating System DetailsNo response SQLModel Version0.0.16 Python VersionPython 3.12.2 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
here is the problem. from future import annotations if TYPE_CHECKING: to keep the type, idk why, all was broken. removing it, all works. |
Beta Was this translation helpful? Give feedback.
To help those who find this and puzzle over the discussion:
To sort out massive errors with SQLModel / SQLAlchemy, the "from future import annotations" statement in the file will completely bork SQLAlchemy with errors leading you to think the relationships are based on generic types.
DO NOT USE from future import annotations AND SQL MODEL
I took this line out after hours of trying things, and SQLModel started working again.