简体   繁体   English

如何修复烧瓶中的sqlalchemy.exc.NoForeignKeysError?

[英]How to fix sqlalchemy.exc.NoForeignKeysError in flask?

I have this error: 我有这个错误:

sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child tables on relationship User.car - there are no foreign keys linking these tables via secondary table 'user'. sqlalchemy.exc.NoForeignKeysError:无法确定关系User.car的父/子表之间的联接条件-没有通过外部表“ user”链接这些表的外键。 Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify 'primaryjoin' and 'secondaryjoin' expressions. 确保引用列与ForeignKey或ForeignKeyConstraint关联,或指定“ primaryjoin”和“ secondaryjoin”表达式。

127.0.0.1 - - [26/Jul/2019 23:05:40] "GET /user HTTP/1.1" 500 - 127.0.0.1--[26 / Jul / 2019 23:05:40]“ GET / user HTTP / 1.1” 500-

The following is my program 以下是我的程序

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship

Base = declarative_base()


class User(Base):

    __tablename__ = 'user'

    user_id = Column(Integer)
    passport_number = Column(String(8),  nullable=False, primary_key=True)
    user_email = Column(String(10), nullable=False)
    user_name = Column(String(10), nullable=False)

    car = relationship('Car', secondary='user')


class Car(Base):
    __tablename__ = 'car'

    car_number = Column(Integer, primary_key=True, nullable=False)
    car_model = Column(String(10), nullable=False)
    user_passport_number = Column(String(8), ForeignKey('user.passport_number'), primary_key=True)

    part = relationship('Part', secondary='car')


class Part(Base):

    __tablename__ = 'part'

    part_name = Column(String(10), primary_key=True, nullable=False)
    part_price = Column(Integer, nullable=False)
    car_number = Column(Integer, ForeignKey('car.car_number'), primary_key=True)

From the error message below: 从以下错误消息中:

...there are no foreign keys linking these tables via secondary table 'user'... ...没有通过外表'user'链接这些表的外键...

we understand that the error might be happening in the setting of the secondary table. 我们知道该错误可能是在辅助表的设置中发生的。 Per the docs : 根据文档

The default behavior of relationship() when constructing a join is that it equates the value of primary key columns on one side to that of foreign-key-referring columns on the other. 构造联接时,relationship()的默认行为是,它使一侧的主键列的值等于另一侧的外键引用列的值。 We can change this criterion to be anything we'd like using the primaryjoin argument, as well as the secondaryjoin argument in the case when a “secondary” table is used. 我们可以使用primaryjoin参数将这个标准更改为我们想要的任何值,在使用“ secondary”表的情况下也可以使用secondaryjoin参数。

Therefore, and assuming that you a have a one-to-many or one-to-one relationship between User and Car , you do not seem to need the secondary parameter: 因此,假设您在UserCar之间具有一对多一对一的关系,那么您似乎不需要secondary参数:

class User(Base):

    __tablename__ = 'user'

    user_id = Column(Integer)
    passport_number = Column(String(8),  nullable=False, primary_key=True)
    user_email = Column(String(10), nullable=False)
    user_name = Column(String(10), nullable=False)

    car = relationship('Car', backref='User') #removed secondary relationship

Also, note that you are passing a primary_key constraint to the foreign key field, which is used only in one-to-one relationships, in case it was not intentional. 此外,请注意,您正在将primary_key约束传递给外键字段,该外键字段仅在一对一关系中使用,以防万一。 In case it was, then the docs show a simple example for the one-to-one tables: 如果是这样,则文档显示一对一表的简单示例:

class User(Base):
    __tablename__ = 'user'
    passport_number = Column(String(8),  nullable=False, primary_key=True)
    car = relationship("Car", uselist=False, back_populates="car")

class Car(Base):
    __tablename__ = 'car'
    id = Column(Integer, primary_key=True)
    user_passport_number = Column(String(8), ForeignKey('user.passport_number'))
    user = relationship("User", back_populates="car")

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

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