Skip to content

How to set cascade option in sqlmodel, I want to delete all the childrens when parent is deleted #213

@basiltt

Description

@basiltt

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the SQLModel documentation, with the integrated search.
  • I already searched in Google "How to X in SQLModel" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to SQLModel but to Pydantic.
  • I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

class User(UserBase, table=True):
    id: int = Field(primary_key=True, index=True)
    created_datetime: datetime = Field(sa_column=Column(DateTime(timezone=True), nullable=False, default=func.now()))
    is_phone_verified: Optional[bool] = Field(default=False)
    is_email_verified: Optional[bool] = Field(default=False)
    is_password_reset_pending: Optional[bool] = Field(default=False)
    addresses: List["UserAddress"] = Relationship(back_populates="useraddress")


class UserAddress(UserAddressBase, table=True):
    id: int = Field(primary_key=True, index=True)
    user_id: int = Field(foreign_key="user.id", nullable=False, index=True)
    is_primary_address: Optional[bool] = Field(default=True)
    geo_location: Optional[str] = Field(
        sa_column=Column(Geometry(geometry_type='POINT', srid=4326), nullable=True))
    added_datetime: datetime = Field(sa_column=Column(DateTime(timezone=True), nullable=False, default=func.now()))
    team: Optional[User] = Relationship(back_populates="addresses")

Description

I couldn't find any samples to delete the children's of a parent when it is deleted.

Operating System

Windows

Operating System Details

Windows 10 20H2

SQLModel Version

sqlmodel==0.0.6

Python Version

Python 3.9.9

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions