admin管理员组

文章数量:1389762

I am using python 3.12, the latest versions of flask, flask-sqlalchemy and sqlalchemy.

My project has a user table with an id field as in:

class User(Base):
    __tablename__ = 'user'

    id: Mapped[int] = mapped_column(
        Integer,
        primary_key=True
    )

The user table has a one-to-many relationship with a registration table. There is a registration table with an id field and a foreign relation back to the user table using a user.id as in:

class Registration(Base):
    __tablename__ = 'registration'

    id: Mapped[int] = mapped_column(
        Integer,
        primary_key=True
    )
    user_id: Mapped[int] = mapped_column(
            ForeignKey('user.id')
        )

The registration table has a one-to-one relationship with an event. There is an event table with an id that has a foreign relation back to the registration table with a registration.id value as in:

class Event(Base):
    __tablename__ = 'event'

    id: Mapped[int] = mapped_column(
        Integer,
        primary_key=True
    )
    user_id: Mapped[int] = mapped_column(
            ForeignKey('registration.id')
        )

If I delete a user then I want to delete all registrations belonging to that user without deleting the events. Likewise, if I delete just a registration then I want just the registration to be deleted but not the event or user.

How do I structure the ForeignKey() definitions to accomplish this?

本文标签: