On Fri, Nov 11, 2016 at 6:59 AM, Alfred Soeng <xiaochang...@gmail.com> wrote:
> It's the issue in the inheritance:
> What I want to do is like this:
> class Employee(Base):
>     __tablename__ = 'employee'
>     id = Column(
>         Integer,
>         primary_key=True,
>     )
>     type_name = Column(DBT.STRING)
>     __mapper_args__ = {'polymorphic_on': 'type_name'}
>
>
> class Teacher(Employee):
>     __tablename__ = 'teacher'
>     id = Column(
>         Integer,
>         ForeignKey("employee.id"),
>         primary_key=True,
>     )
>     related_student = relationship(
>         "Student",
>         uselist=False,
>         back_populates="teacher")
>     __mapper_args__ = {
>         'polymorphic_identity': 'TEACHER',
>         'inherit_condition': (id == Employee.id),
>     }
>
>
> class Student(Employee):
>     __tablename__ = 'student'
>     id = Column(
>         Integer,
>         ForeignKey("employee.id"),
>         primary_key=True,
>     )
>     teacher_id = Column(Integer, ForeignKey("teacher.id"))
>     teacher = relationship(
>         "Teacher",
>         foreign_keys=[teacher_id],
>         back_populates="related_student"
>     )
>     __mapper_args__ = {
>         'polymorphic_identity': 'STUDENT',
>         'inherit_condition': (id == Employee.id),
>     }
>
> If I didn't specify the foreign_keys for Teacher relationship, it will say
> sqlalchemy.exc.AmbiguousForeignKeysError: Could not determine join condition
> between parent/child tables on relationship Teacher.related_student - 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.
>
> After I setting the foreign_keys, like below(add foreign_keys for
> related_student relationship):
> class Employee(Base):
>     __tablename__ = 'employee'
>     id = Column(
>         Integer,
>         primary_key=True,
>     )
>     type_name = Column(DBT.STRING)
>     __mapper_args__ = {'polymorphic_on': 'type_name'}
>
>
> class Teacher(Employee):
>     __tablename__ = 'teacher'
>     id = Column(
>         Integer,
>         ForeignKey("employee.id"),
>         primary_key=True,
>     )
>     related_student = relationship(
>         "Student",
>         uselist=False,
>         foreign_keys=[id]
>         back_populates="teacher")
>     __mapper_args__ = {
>         'polymorphic_identity': 'TEACHER',
>         'inherit_condition': (id == Employee.id),
>     }
>
>
> class Student(Employee):
>     __tablename__ = 'student'
>     id = Column(
>         Integer,
>         ForeignKey("employee.id"),
>         primary_key=True,
>     )
>     teacher_id = Column(Integer, ForeignKey("teacher.id"))
>     teacher = relationship(
>         "Teacher",
>         foreign_keys=[teacher_id],
>         back_populates="related_student"
>     )
>     __mapper_args__ = {
>         'polymorphic_identity': 'STUDENT',
>         'inherit_condition': (id == Employee.id),
>     }
>
> The error is:
> sqlalchemy.exc.ArgumentError: Student.teacher and back-reference
> Teacher.related_student are both of the same direction symbol('MANYTOONE').
> Did you mean to set remote_side on the many-to-one side ?
>
> Is there any way to solve the problem keeping the inheritance structure and
> 1:1 relationship?
> Thanks so much.
>

Here are your relationship definitions:

    teacher = relationship(
        "Teacher",
        foreign_keys=[teacher_id],
        back_populates="related_student"
    )


    related_student = relationship(
        "Student",
        uselist=False,
        foreign_keys=[id]   # <-- this is incorrect
        back_populates="teacher")


The "teacher" relationship looks correct - teacher_id is indeed a
foreign key pointing to the Teacher.id column. But the
"related_student" relationship is wrong - Teacher.id is not a foreign
key (for the purposes of this relationship). I'm not sure if you need
the foreign_keys argument here, but if you do, it should probably be
[Student.teacher_id].

Simon

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to