Flask 中的多个外键 SqlAlchemy 连接错误
Multiple Foreign key SqlAlchemy join error in flask
我有两个类。一个机构可以有多个学生。因此,我有两个 类,如下所示:
class University(db.Model):
country = db.Column(db.String(120), primary_key=True)
name = db.Column(db.String(120), primary_key=True)
students = db.relationship('Student', backref='phd_granting_university', lazy='dynamic')
class Student(db.Model):
name = db.Column(db.String(120), primary_key=True)
phd_graduation_year = db.Column(db.Integer, primary_key=True)
phd_granting_university_name = db.Column(db.String(120), db.ForeignKey('university.name'), primary_key=True)
phd_granting_university_country = db.Column(db.String(120), db.ForeignKey('university.country'), primary_key=True)
我收到以下错误:
Could not determine join condition between parent/child tables on
relationship University.students - there are multiple foreign key
paths linking the tables. Specify the 'foreign_keys' argument,
providing a list of those columns which should be counted as
containing a foreign key reference to the parent table.
复合外键必须是 declared at the table level。要在 SQLAlchemy 的 ORM 中执行此操作,请在 __table_args__
属性中声明约束:
class Student(db.Model):
name = db.Column(db.String(120), primary_key=True)
phd_graduation_year = db.Column(db.Integer, primary_key=True)
phd_granting_university_name = db.Column(db.String(120), primary_key=True)
phd_granting_university_country = db.Column(db.String(120), primary_key=True)
__table_args__ = (
db.ForeignKeyConstraint(
[phd_granting_university_name, phd_granting_university_country],
['university.name', 'university.country'],
),
)
这将生成此 DDL:
CREATE TABLE students (
name VARCHAR(120) NOT NULL,
phd_graduation_year INTEGER NOT NULL,
phd_granting_university_name VARCHAR(120) NOT NULL,
phd_granting_university_country VARCHAR(120) NOT NULL,
PRIMARY KEY (name, phd_graduation_year, phd_granting_university_name, phd_granting_university_country),
FOREIGN KEY(phd_granting_university_name, phd_granting_university_country) REFERENCES university (name, country)
)
我有两个类。一个机构可以有多个学生。因此,我有两个 类,如下所示:
class University(db.Model):
country = db.Column(db.String(120), primary_key=True)
name = db.Column(db.String(120), primary_key=True)
students = db.relationship('Student', backref='phd_granting_university', lazy='dynamic')
class Student(db.Model):
name = db.Column(db.String(120), primary_key=True)
phd_graduation_year = db.Column(db.Integer, primary_key=True)
phd_granting_university_name = db.Column(db.String(120), db.ForeignKey('university.name'), primary_key=True)
phd_granting_university_country = db.Column(db.String(120), db.ForeignKey('university.country'), primary_key=True)
我收到以下错误:
Could not determine join condition between parent/child tables on relationship University.students - there are multiple foreign key paths linking the tables. Specify the 'foreign_keys' argument, providing a list of those columns which should be counted as containing a foreign key reference to the parent table.
复合外键必须是 declared at the table level。要在 SQLAlchemy 的 ORM 中执行此操作,请在 __table_args__
属性中声明约束:
class Student(db.Model):
name = db.Column(db.String(120), primary_key=True)
phd_graduation_year = db.Column(db.Integer, primary_key=True)
phd_granting_university_name = db.Column(db.String(120), primary_key=True)
phd_granting_university_country = db.Column(db.String(120), primary_key=True)
__table_args__ = (
db.ForeignKeyConstraint(
[phd_granting_university_name, phd_granting_university_country],
['university.name', 'university.country'],
),
)
这将生成此 DDL:
CREATE TABLE students (
name VARCHAR(120) NOT NULL,
phd_graduation_year INTEGER NOT NULL,
phd_granting_university_name VARCHAR(120) NOT NULL,
phd_granting_university_country VARCHAR(120) NOT NULL,
PRIMARY KEY (name, phd_graduation_year, phd_granting_university_name, phd_granting_university_country),
FOREIGN KEY(phd_granting_university_name, phd_granting_university_country) REFERENCES university (name, country)
)