V
V
Viktor Korovin2015-10-14 23:48:12
Python
Viktor Korovin, 2015-10-14 23:48:12

Multiple Many2One relationships in SQLAlchemy?

class Person(BaseTable):
    __tablename__ = "persons"
    id = Column(Integer, primary_key=True)

class F(BaseTable):
    __tablename__ = "fabcs"
    abc_id = Column(Integer, ForeignKey("persons.id"))
    a = relationship('Person')
    b = relationship('Person')
    c = relationship('Person')

With this code, when you try to assign different values ​​to a, b and c, one thing remains in all. How to make it so that a, b and c can be different?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Viktor Korovin, 2015-10-15
@Ktulhy

Understood it. The casket just opened:
docs.sqlalchemy.org/en/rel_1_0/orm/join_conditions.html
In this case, it will look like this:

class F(BaseTable):
    __tablename__ = "fabcs"
    a_id = Column(Integer, ForeignKey("persons.id"))
    b_id = Column(Integer, ForeignKey("persons.id"))
    c_id = Column(Integer, ForeignKey("persons.id"))
    a = relationship('Person', foreign_keys=[a_id])
    b = relationship('Person', foreign_keys=[b_id])
    c = relationship('Person', foreign_keys=[c_id])

A
angru, 2015-10-15
@angru

from documentation :

class Parent(Base):
    __tablename__ = 'parent'
    id = Column(Integer, primary_key=True)
    children = relationship("Child")

class Child(Base):
    __tablename__ = 'child'
    id = Column(Integer, primary_key=True)
    parent_id = Column(Integer, ForeignKey('parent.id'))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question