First Check
Commit to Help
Example Code
# ModelsclassAssemblyEPDLink(SQLModel, table=True):
assembly_id: Optional[str] =Field(default=None, foreign_key="assembly.id", primary_key=True)
epd_id: Optional[str] =Field(default=None, foreign_key="epd.id", primary_key=True)
conversion_factor: float=1.0assembly: "Assembly"=Relationship(back_populates="layer_links")
epd: "EPD"=Relationship(back_populates="assembly_links")
classAssembly(SQLModel, table=True):
"""Assembly database class"""id: Optional[str] =Field(default_factory=string_uuid, primary_key=True)
name: str=Field(index=True)
category: strlife_time: float=50meta_fields: dict=Field(default=dict, sa_column=Column(JSON), nullable=False)
unit: str="M2"conversion_factor: float=1.0layer_links: List[AssemblyEPDLink] =Relationship(back_populates="assembly")
classEPD(SQLModel, table=True):
"""EPD database class"""id: Optional[str] =Field(default_factory=string_uuid, primary_key=True)
name: str=Field(index=True)
category: strgwp_by_phases: dict=Field(default=dict, sa_column=Column(JSON), nullable=False)
version: strexpiration_date: datedate_updated: datesource: strassembly_links: List[AssemblyEPDLink] =Relationship(back_populates="epd")
# Strawberry Mutation@strawberry.typeclassMutation:
@strawberry.mutation(permission_classes=[IsAuthenticated])asyncdefadd_assembly(
self,
info: Info,
name: str,
category: str,
life_time: float|None=50,
meta_fields: Optional[JSON] =None,
layers: Optional[JSON] =None,
conversion_factor: float|None=1,
) ->GraphQLAssembly:
ifmeta_fieldsisNone:
meta_fields= {}
session=info.context.get("session")
assembly=Assembly(
name=name,
category=category,
life_time=life_time,
conversion_factor=conversion_factor,
meta_fields=meta_fields,
)
iflayers:
query=select(EPD).where(col(EPD.id).in_([layer.get("id") forlayerinlayers]))
epds=awaitsession.exec(query)
epds=epds.all()
forepdinepds:
link=AssemblyEPDLink(assembly=assembly, epd=epd, conversion_factor=2)
session.add(link)
else:
session.add(assembly)
awaitsession.commit()
awaitsession.refresh(assembly)
returnassemblyDescription
- I start my test
- Set up a blank database and run
SQLModel.metadata.create_all(bind=engine) - I create three EPDs and save them to the database
- Then I post a new mutation to create a new Assembly with those EPDs in "layers"
- When I try to commit the AssemblyEPDLink to the session the database throws an error:
'asyncpg.exceptions.NotNullViolationError': null value in column "assembly_id" of relation "assemblyepdlink" violates not-null constraint
Operating System
Linux
Operating System Details
Ubuntu 22.04
SQLModel Version
0.0.6
Python Version
3.10
Additional Context
I have tried to follow the steps outlined in the docs, adapted to my situation, but somehow the link model doesn't want to save to the database.
When I create an Assembly without a link model everything works fine.
I have a setup with FastAPI, Strawberry, SQLModel and postgres.
First Check
Commit to Help
Example Code
Description
SQLModel.metadata.create_all(bind=engine)'asyncpg.exceptions.NotNullViolationError': null value in column "assembly_id" of relation "assemblyepdlink" violates not-null constraintOperating System
Linux
Operating System Details
Ubuntu 22.04
SQLModel Version
0.0.6
Python Version
3.10
Additional Context
I have tried to follow the steps outlined in the docs, adapted to my situation, but somehow the link model doesn't want to save to the database.
When I create an Assembly without a link model everything works fine.
I have a setup with FastAPI, Strawberry, SQLModel and postgres.