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?
本文标签:
版权声明:本文标题:python 3.x - How do I structure sqlalchemy cascade delets to remove what I want removed and leave the rest? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744732021a2622103.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论