I separately created a Postgresql database with a schema named “geog” and two tables named projections (id, epsg_code, unit_id) and units (id, name).
So in my code I have:
- units.py
...
class Unit(SQLModel, table=True):
__tablename__ = "units"
metadata = {schema="geog"}
id: Optional[int] = Field(primary_key=True)
name: str
projections: List["Projection"] = Relationship(back_populates="units")
- projections.py
...
class Projections(SQLModel, table=True):
__tablename__ = "projections"
metadata = {schema="geog"}
id: Optional[int] = Field(primary_key=True)
epsg_code: str
unit_id: int = Field(foreign_key="units.id")
units: Optional["Unit"] = Relationship(back_populates="projections")
And in the DB I manually declared:
- in the units table:
- pk_units as a pk related to the units.id col
- in the projections table:
- pk_projections as a pk related to the projections.id col
- fk_projections_units_unit_id as a fk related to the projections.unit_id and units.id
But each time I use my CRUD (whatever the function), I’ve got the following error:
sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child tables on relationship Projection.units - there are no foreign keys linking these tables. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.