Foreign key with ondelete set null #613
-
First Check
Commit to Help
Example Codeclass UUIDModel(SQLModel):
uuid: uuid_pkg.UUID = Field(
default_factory=uuid_pkg.uuid4,
primary_key=True,
index=True,
nullable=False,
sa_column_kwargs={
"server_default": text("gen_random_uuid()"),
"unique": True,
},
)
class Client(UUIDModel, SQLModel):
number_login: str = Field(max_length=20, nullable=False, index=True)
uuid_fiber_appointment: Optional[uuid_pkg.UUID] = Field(
foreign_key="fiber_appointment.uuid",
nullable=True,
)
fiber_appointment: "FiberAppointment" = Relationship(
back_populates="clients",
sa_relationship_kwargs={"lazy": "selectin"},
)
class FiberAppointment(
UUIDModel,
SQLModel,
table=True,
):
__tablename__ = "fiber_appointment"
appointment_datetime: datetime
clients: List["Client"] = Relationship(
back_populates="fiber_appointment",
) DescriptionI need to delete a list of appointments and that the clients that had those appointments, in the foreign key field (uuid_fiber_appointment) be set to null Operating SystemmacOS Operating System DetailsNo response SQLModel Version0.0.8 Python VersionPython 3.10.11 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Answered by
YuriiMotov
Aug 19, 2025
Replies: 2 comments
-
Have you tried adding class Client(UUIDModel, SQLModel):
number_login: str = Field(max_length=20, nullable=False, index=True)
uuid_fiber_appointment: Optional[uuid_pkg.UUID] = Field(
foreign_key="fiber_appointment.uuid",
nullable=True,
)
fiber_appointment: "FiberAppointment" = Relationship(
back_populates="clients",
sa_relationship_kwargs={"lazy": "selectin", "cascade": ""},
) Try it |
Beta Was this translation helpful? Give feedback.
0 replies
-
Related section of docs: https://sqlmodel.tiangolo.com/tutorial/relationship-attributes/cascade-delete-relationships/#ondelete-with-set-null |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
YuriiMotov
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Related section of docs: https://sqlmodel.tiangolo.com/tutorial/relationship-attributes/cascade-delete-relationships/#ondelete-with-set-null