On 11/10/2016 06:52 PM, Alfred Soeng wrote:
> from sqlalchemy.ext.declarative import declarative_base
> from sqlalchemy import Column, Integer, ForeignKey
> from sqlalchemy.orm import relationship
>
> Base = declarative_base()
>
>
> class Teacher(Base):
> __tablename__ = 'teacher'
> id = Column(Integer, ForeignKey("
student.id <
http://student.id>"),
> primary_key=True)
> student = relationship("Student", foreign_keys=[id], uselist=False,
> back_populates="teacher")
>
>
> class Student(Base):
> __tablename__ = 'student'
> id = Column(Integer, primary_key=True)
> teacher_id = Column(Integer, ForeignKey("
teacher.id
> <
http://teacher.id>"))
> teacher = relationship("Teacher", foreign_keys=[teacher_id],
> back_populates="student")
>
> I define the 2 classes and they are one to one relation.
> But each time the binary run said:
>
> sqlalchemy.exc.ArgumentError: Teacher.student and back-reference
> Student.teacher are both of the same direction symbol('MANYTOONE'). Did
> you mean to set remote_side on the many-to-one side ?
>
> Can I ask what's wrong with it?
you have conflicting foreign_keys settings, each one states the opposite
relationship. Only "teacher_id" qualifies for foreign_keys here (would
be set on both sides), but there is no need to set this parameter at all
as you already have ForeignKey present on the actual teacher_id Column
object.
>
> Thanks so much.
>
> --
> 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+...@googlegroups.com
> <mailto:
sqlalchemy+...@googlegroups.com>.
> To post to this group, send email to
sqlal...@googlegroups.com
> <mailto:
sqlal...@googlegroups.com>.
> Visit this group at
https://groups.google.com/group/sqlalchemy.
> For more options, visit
https://groups.google.com/d/optout.