无法通过多对多连接Sqlalchemy确定连接条件

时间:2017-03-26 02:54:28

标签: python sqlalchemy flask-sqlalchemy

我在SQLAlchemy中收到以下错误:

  

无法确定父/子表之间的连接条件   relationship.user - 没有链接这些的外键   表。确保引用列与a关联   ForeignKey或ForeignKeyConstraint,或指定' primaryjoin'   表达

我正在尝试在User和Event表之间创建多对多关系。目标是将用户添加为"会员"一个事件。该活动也是"拥有"一个用户,但这是一个不同的一对多关系。

class Membership(db.Model):
    __tablename__ = 'event_membership'

    id = db.Column(db.Integer, primary_key = True, unique=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True),
    event_id = db.Column(db.Integer, db.ForeignKey('event.id'), primary_key=True),
    time_created = db.Column(db.DateTime,nullable = False, default = func.now())

    user = db.relationship("Event",back_populates="memberships",foreign_keys=[])
    event = db.relationship("User",back_populates="members")


class User(db.Model,UserMixin):
    __tablename__ = 'user'
    id = db.Column(db.Integer(),primary_key =True)
    firstname = db.Column(db.String(50))
    lastname = db.Column(db.String(50))
    password = db.Column(db.String(255))
    username = db.Column(db.String(50), index = True)
    email = db.Column(db.String(255), index = True)
    create_date = db.Column(db.DateTime())
    questions = db.relationship('Question',backref='user',lazy='dynamic')
    events = db.relationship('Event',backref='owner',lazy='dynamic')
    admin = db.Column(db.Boolean,nullable=False,default=False)
    #moderating_events = db.Column(db.Integer(),db.ForeignKey('event.id'))
    authenticated = db.Column(db.Boolean,nullable=False,default=False)
    lastseen = db.Column(db.DateTime)
    validated = db.Column(db.Boolean,nullable=False,default=False)

    memberships = db.relationship("Membership", back_populates= "user" )

class Event(db.Model):
    __tablename__ = 'event'
    id = db.Column(db.Integer(),primary_key = True)
    name = db.Column(db.String(255))
    event_code = db.Column(db.String(30))
    description = db.Column(db.Text())
    owner_id = db.Column(db.Integer(),db.ForeignKey('user.id'))
    #owner = db.relationship("User",back_populates="events")
    questions = db.relationship('Question',backref='event',lazy='dynamic')
    create_date = db.Column(db.DateTime())
    active_date = db.Column(db.DateTime())
    active = db.Column(db.Boolean())
    meeting_time = db.Column(db.DateTime())
    #moderators = db.relationship('User', backref='event_moderator',lazy='dynamic')
    moderate_questions = db.Column(db.Boolean,nullable=False,default=True)
    allow_anonymous = db.Column(db.Boolean,nullable=False,default=True)

    members = db.relationship("Membership", back_populates="event")

1 个答案:

答案 0 :(得分:1)

这个错误对我来说并不陌生!我在两个月前面对它,我以这种方式解决它,以达到很多很多关系 你必须编辑你的关联表“会员”。 然后你可以将它作为用户

中关系的次要参数传递

试试这段代码:

Membership =db.Table(
'Membership',
db.Column('User_id',db.Integer,db.ForeignKey('User.id')),
db.Column('Event_id', db.Integer, db.ForeignKey('Event.id')),

并在User Table表中添加:

 Events = relationship("Event",
                secondary=Membership)

在SqlALchemy的官方文档中找到更多here