SQLAlchemy - 指向同一个表的多个外键相同的属性

时间:2017-06-08 11:25:37

标签: python sqlite sqlalchemy flask-sqlalchemy entity-relationship

我的数据库结构......

class Person(db.Model):
    id = db.Column(db.Integer, primary_key=True)

    user = db.relationship("BankSlip", back_populates="person_user")
    reference = db.relationship("BankSlip", back_populates="person_reference")

class BankSlip(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)

    person_user_id = db.Column(db.Integer, db.ForeignKey(Person.id))
    person_ref_id = db.Column(db.Integer, db.ForeignKey(Person.id))

    person_user = db.relationship("Person", back_populates="user", uselist=False, foreign_keys=[person_user_id])
    person_reference = db.relationship("Person", back_populates="reference", uselist=False, foreign_keys=[person_ref_id])

使用Flask-SQLAlchemy

在sqlite上运行时出现以下错误
  

sqlalchemy.exc.AmbiguousForeignKeysError:无法确定关系Person.user上的父/子表之间的连接条件 - 有多个链接表的外键路径。指定'foreign_keys'参数,提供应列为包含父表的外键引用的列的列表。

这是我的pip freeze

appdirs==1.4.3
APScheduler==3.3.1
bcrypt==3.1.3
blinker==1.4
cffi==1.9.1
click==6.7
cssselect==1.0.1
cssutils==1.0.2
Flask==0.12
Flask-Login==0.4.0
Flask-Mail==0.9.1
Flask-Principal==0.4.0
Flask-SQLAlchemy==2.2
Flask-WTF==0.14.2
gunicorn==19.7.1
itsdangerous==0.24
Jinja2==2.9.5
lxml==3.7.3
MarkupSafe==1.0
nose==1.3.7
packaging==16.8
pkg-resources==0.0.0
premailer==3.0.1
pycparser==2.17
pyparsing==2.2.0
python-dateutil==2.6.0
pytz==2017.2
requests==2.13.0
schedule==0.4.2
six==1.10.0
SQLAlchemy==1.1.6
tzlocal==1.4
uWSGI==2.0.15
Werkzeug==0.12.1
WTForms==2.1

修改:一个BankSlip可以有一个用户和一个引用....它应该是parent -> childBankSlip -> User或{BankSlip -> Reference的一对一关系1}}。所以,一个孩子可以有多个父母!

1 个答案:

答案 0 :(得分:7)

按照错误消息的说明提供所需的foreign_keys参数:

class Person(db.Model):
    id = db.Column(db.Integer, primary_key=True)

    user = db.relationship("BankSlip", foreign_keys='BankSlip.person_user_id', back_populates="person_user")
    reference = db.relationship("BankSlip", foreign_keys='BankSlip.person_ref_id', back_populates="person_reference")

使用Declarative you can define the foreign keys as a string,这将有助于解决循环依赖。或者,您可以使用backref而不是back_populates:

class Person(db.Model):
    id = db.Column(db.Integer, primary_key=True)


class BankSlip(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)

    person_user_id = db.Column(db.Integer, db.ForeignKey(Person.id))
    person_ref_id = db.Column(db.Integer, db.ForeignKey(Person.id))

    person_user = db.relationship("Person", backref="user", uselist=False, foreign_keys=[person_user_id])
    person_reference = db.relationship("Person", backref="reference", uselist=False, foreign_keys=[person_ref_id])

请注意,您的uselist=False位于关系的错误末端,或者它是多余的,因为Person可以被多个BankSlips引用。它属于人的一面,所以:

from sqlalchemy.orm import backref

...
    person_user = db.relationship("Person", backref=backref("user", uselist=False), foreign_keys=[person_user_id])
    person_reference = db.relationship("Person", backref=backref("reference", uselist=False), foreign_keys=[person_ref_id])