Creating a relationship when no direct join exists

时间:2016-07-11 19:40:04

标签: python sqlalchemy

I have three models that have chaining foreign-keys. I'd like to create a relationship between the first model and the last model using those intermediary foreign-keys as joins.

The code looks something like the below:

class A(Model):
    # Hypothetical relationship I'd like to have.
    c_relationship = relationship('C', backref='A')


class B(Model):
    a_id = db.Column(db.Integer, db.ForeignKey('a.id'))


class C(Model):
    # C is indirectly related to A through B.
    b_id = db.Column(db.Integer, db.ForeignKey('b.id'))

0 个答案:

没有答案