python - SQLAlchemy 中继承类之间的正确引用

标签 python python-3.x sqlalchemy flask-sqlalchemy

假设我们有一些模板,我们稍后希望创建多个对象,基于这个模板,保留到这个对象的链接,也有反向链接来调用基于这个模板创建的对象,所以我们将实现这样的类:

class BasicSentence(db.Model):
    __tablename__ = 'basic_sentences'

    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(25))
    type = db.Column(db.String(50))         
    __mapper_args__ = {
        'polymorphic_identity': 'basic_sentence',
        'polymorphic_on': type
    }

sentence_layout.py:

class SentenceLayout(BasicSentence, db.Model):
    __tablename__ = 'sentences_layouts'

    id = db.Column(db.Integer, db.ForeignKey('basic_sentences.id'), primary_key=True)

    # RELATIONSHIP
    sentenences = relationship("Sentence",
                                  back_populates="sentence_layout")

    __mapper_args__ = {
        'polymorphic_identity': 'sentence_layout',
        'inherit_condition': id == BasicSentence.id
    }

句子.py:

class Sentence(BasicSentence, db.Model):
    __tablename__ = 'advertisements'

    id = db.Column(db.Integer, db.ForeignKey('basic_sentences.id'), primary_key=True)

    # Relationships
    sentence_layout_id = db.Column(db.Integer, db.ForeignKey('sentences_layouts.id'))
    sentence_layout = relationship(SentenceLayout, foreign_keys=[sentence_layout_id],
                                        back_populates="advertisements")

    __mapper_args__ = {
        'polymorphic_identity': 'sentence',
        'inherit_condition': id == BasicSentence.id,
    }

问题是这会导致:

sqlalchemy.exc.AmbiguousForeignKeysError: Can't determine join between 'Join object on basic_sentences(4379708104) and sentences_layouts(4379795752)' and 'Join object on basic_sentences(4379708104) and sentences(4379797488)'; tables have more than one foreign key constraint relationship between them. Please specify the 'onclause' of this join explicitly.

During handling of the above exception, another exception occurred:

sqlalchemy.exc.AmbiguousForeignKeysError: Could not determine join condition between parent/child tables on relationship SentenceLayout.sentences - 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.

Python 3.6、SQLAlchemy 1.2.2

那么在 SQLAlchemy 中继承类之间的正确引用是什么?

最佳答案

您还需要在 SentenceLayout.sentenences 上指定 foreign_keys 参数,而不仅仅是 Sentence.sentence_layout 关系:

sentenences = relationship("Sentence", foreign_keys=Sentence.sentence_layout_id,
                           back_populates="sentence_layout")

由于循环性,您可能需要改用 lambda 或字符串:

sentenences = relationship("Sentence", foreign_keys=lambda: full.reference.to.sentence.Sentence.sentence_layout_id,
                           back_populates="sentence_layout")

sentenences = relationship("Sentence", foreign_keys="Sentence.sentence_layout_id",
                           back_populates="sentence_layout")

关于python - SQLAlchemy 中继承类之间的正确引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50881639/

相关文章:

python - 从 Uber Ludwig 中的图像发出训练模型

python - Flask SQLAlchemy NOT NULL 约束在主键上失败

python - 如何在 sqlalchemy 中映射 postgres EARTH 列

Python zeromq——单个订阅者的多个发布者?

python - 如何在 spaCy 中提取带有关键短语的句子

java - java中的 ">>>"是什么意思?

python - 如何从 DOC(而不是 DOCX)获取 XML?

python - 'is' 运算符对 float 的表现出乎意料

Python无法安装包

python - 为 MS-SQL 创建不区分大小写的 SQLAlchemy 查询