无法向具有多对多关系的 table 添加条目

Can't add an entry to a table with Many-to-Many relationships

我正在尝试添加一个 Art 对象并收到此错误:

sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'mapped class Source->sources'. Original exception was: Could not determine join condition between parent/child tables on relationship Source.arts - there are no foreign keys linking these tables.  Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.

这是我的代码:

art_tags = db.Table('art_tags',
    db.Column('art_id', db.Integer, db.ForeignKey('arts.id')),
    db.Column('tag_id', db.Integer, db.ForeignKey('tags.id'))
)

art_categories = db.Table('art_categories',
    db.Column('art_id', db.Integer, db.ForeignKey('arts.id')),
    db.Column('category_id', db.Integer, db.ForeignKey('categories.id'))
)

class Tag(db.Model):
    __tablename__ = 'tags'
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(255), nullable=False)
    arts = db.relationship('Art', secondary=art_tags, backref='tags', lazy=True)

    def __repr__(self):
        return '<Tag id={} name="{}">'.format(self.id, self.name)

class Source(db.Model):
    __tablename__ = 'sources'
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(255), nullable=False)
    arts = db.relationship('Art', backref='source', lazy=True)

    def __repr__(self):
        return '<Source id={} name="{}"'.format(self.id, self.name)

class Category(db.Model):
    __tablename__ = 'categories'
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(255), nullable=False)
    arts = db.relationship('Art', secondary=art_categories, backref='categories', lazy=True)

    def __repr__(self):
        return '<Category id={} name="{}">'.format(self.id, self.name)

class Art(db.Model):
    __tablename__ = 'arts'
    id = db.Column(db.Integer(), primary_key=True)
    original = db.Column(db.Text, nullable=False, unique=True)
    preview = db.Column(db.Text, nullable=False, unique=True)

    def __repr__(self):
        return '<Art id={}>'.format(self.id)

现在,我设法让这个东西正常工作。

我在 Art 对象上创建了缺失的列:

class Art(db.Model):
    __tablename__ = 'arts'
    ...
    source_id = db.Column(db.Integer(), db.ForeignKey('sources.id'))
    tags = db.relationship('Tag', secondary=art_tags, back_populates='arts')
    categories = db.relationship('Category', secondary=art_categories, back_populates='arts')
    ...

然后我将其他 类(表格)中的所有 backref= 更改为 back_populates=

class Tag(db.Model):
    ...
    arts = db.relationship('Art', secondary=art_tags, back_populates='tags')
    ...

class Source(db.Model):
    ...
    # Here we have to leave backref, because this isn't Many-to-Many, but Many-to-One
    arts = db.relationship('Art', backref='source')
    ...

class Category(db.Model):
    ...
    arts = db.relationship('Art', secondary=art_categories, back_populates='categories')
    ...

然后,Art对象创建成功。