-
First Check
Commit to Help
Example Code## code example
db_unit = models.CreateUnit(
unit = unit.unit,
unit_code = unit.unit_code,
unit_multiples = unit.unit_multiples
)
db_unit.createunittype = unit_types
session.add(db_unit)
await session.commit()
await session.refresh(db_unit)
## models
class CreateUnitType(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
mass: str
volume: int
length: int
electric_current: str
units: List["CreateUnit"] = Relationship(back_populates='createunittype')
class CreateUnit(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
unit: str
unit_code: str
unit_multiples: int
unit_type_id: Optional[int] = Field(default=None, foreign_key="createunittype.id")
createunittype: Optional[CreateUnitType] = Relationship(back_populates="units") DescriptionI have two different models CreateUnitType, CreateUnit attached with unit_type_id field as foreign key to CreateUnitType table and the whole database is in postgres. I want to establish the Relationship feature but somehow whenever I try to run the query I get this following error -
Operating SystemLinux Operating System DetailsNo response SQLModel Version0.0.4 Python Version3.7.8 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I'm not sure what exactly the problem with your code is. One small issue is that you're instantiating from typing import Optional, List
from sqlmodel import Field, Session, SQLModel, create_engine, select, Relationship
class CreateUnitType(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
mass: str
units: List["CreateUnit"] = Relationship(back_populates="createunittype")
class CreateUnit(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
unit: str
unit_type_id: Optional[int] = Field(default=None, foreign_key="createunittype.id")
createunittype: Optional[CreateUnitType] = Relationship(back_populates="units")
sqlite_file_name = "database.db"
sqlite_url = f"sqlite:///{sqlite_file_name}"
engine = create_engine(sqlite_url, echo=True)
if __name__ == "__main__":
SQLModel.metadata.create_all(engine)
unit = CreateUnit(unit="unit", createunittype=CreateUnitType(mass="1kg"))
print(unit)
with Session(engine) as session:
session.add(unit)
session.commit()
with Session(engine) as session:
statement = select(CreateUnit)
results = session.exec(statement)
for unit in results:
print(unit)
statement = select(CreateUnitType)
results = session.exec(statement)
for unit in results:
print(unit) Output:
Granted I tested this with a SQLite DB but I'm not sure that it should make a different (afaik Postgres should behave the same way). Can you try the code I've pasted above and let me know if it works for you? |
Beta Was this translation helpful? Give feedback.
I'm not sure what exactly the problem with your code is. One small issue is that you're instantiating
db_unit
before you've declaredCreateUnit
. I've used the code below to try to replicate and I was unsuccessful: