简体   繁体   English

相同型号中的烧瓶-sqlalchemy 一对一和多对一

[英]flask-sqlalchemy one to one and many to one in same models

I'm trying to make an invitation system where one User has an InvitationToken that can be sent to multiple invitees .我正在尝试制作一个邀请系统,其中一个User有一个InvitationToken可以发送给多个invitees Each User can have only one token (one-to-one), as well as be an invitee in multiple tokens (many-to-one).每个User只能拥有一个令牌(一对一),也可以是多个令牌中的被邀请者(多对一)。

This is what I have so far:这是我到目前为止所拥有的:

InvitationToken.py邀请令牌.py

class InvitationToken(db.Model, SerializeMixin, TimestampMixin):
    __tablename__ = "invitation_token"

    """
    The relationship to the User model. An InvitationToken can have many Users as invitees.
    """
    invitees = db.relationship(
        "User", back_populates="invitee_token", foreign_keys="User.invitee_token_id"
    )

    """
    The relationship to the User model. An InvitationToken can have only one User.
    """
    user_id = db.Column(UUID(as_uuid=True), db.ForeignKey("app_user.id"), index=True)
    user = db.relationship(
        "User",
        back_populates="invitation_token",
        foreign_keys=[user_id],
        uselist=False,
    )

User.py用户.py

class User(db.Model, TimestampMixin):
    __tablename__ = "app_user"

    """
    The relationship to the InvitationToken model. A User can have only one InvitationToken.
    """
    invitation_token_id = db.Column(
        UUID(as_uuid=True), db.ForeignKey("invitation_token.id"), index=True
    )
    invitation_token = db.relationship(
        "InvitationToken",
        back_populates="user",
        cascade="delete",
        foreign_keys=[invitation_token_id],
        uselist=False,
    )

    """
    The relationship to the InvitationToken model. A User can have only one InvitationToken as invitee.
    """
    invitee_token_id = db.Column(
        UUID(as_uuid=True), db.ForeignKey("invitation_token.id"), index=True
    )
    invitee_token = db.relationship(
        "InvitationToken",
        back_populates="invitees",
        foreign_keys=[invitee_token_id],
    )

I'm getting the following error with my current configuration:我的当前配置出现以下错误:

sqlalchemy.exc.ArgumentError: InvitationToken.user and back-reference User.invitation_token are both of the same direction symbol('MANYTOONE').  Did you mean to set remote_side on the many-to-one side ?

What am I missing?我错过了什么?

I found a solution, although I do not fully understand it.我找到了一个解决方案,虽然我并不完全理解它。 But it works:但它有效:

class InvitationToken(db.Model, SerializeMixin, TimestampMixin):
    __tablename__ = "invitation_token"

    """
    The relationship to the User model. An InvitationToken can have many Users as invitees.
    """
    invitees = db.relationship(
        "User",
        back_populates="invitee_token",
        foreign_keys="User.invitee_token_id",  # Changed this
    )

    """
    The relationship to the User model. An InvitationToken can have only one User.
    """
    user_id = db.Column(UUID(as_uuid=True), db.ForeignKey("app_user.id"), index=True)
    user = db.relationship(
        "User",
        back_populates="invitation_token",
        foreign_keys="User.invitation_token_id",  # Changed this
        uselist=False,
    )

User was left unchanged.用户保持不变。 This got rid of the error and allowed me to run Flask and have the models behave how I wanted.这消除了错误并允许我运行 Flask 并让模型按照我想要的方式运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM