hey there - these mappings are pretty good, as is always the case I cannot predict why an issue is occurring, or usually even understand the issue, without running the code. your code is pretty runnable with a few imports added so that's great. however adding an assertion for the condition you describe "m2.daughter is not None" is not reproducible on my end. Try out the script below and see if you have different results.
from sqlalchemy import Column from sqlalchemy import create_engine from sqlalchemy import ForeignKey from sqlalchemy import Integer from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import relationship from sqlalchemy.orm import Session Base = declarative_base() class Super(Base): __tablename__ = "super" id = Column(Integer, primary_key=True) class Mama(Super): __tablename__ = "mama" id = Column(Integer, ForeignKey("super.id"), primary_key=True) daughter_id = Column(Integer, ForeignKey("daughter.id")) daughter = relationship( "Daughter", foreign_keys=[daughter_id], back_populates="mama" ) class Daughter(Super): __tablename__ = "daughter" id = Column(Integer, ForeignKey("super.id"), primary_key=True) mama = relationship( "Mama", foreign_keys=[Mama.daughter_id], uselist=False, back_populates="daughter", ) e = create_engine("sqlite://", echo=True) Base.metadata.create_all(e) session = Session(e) m1 = Mama() m2 = Mama() d1 = Daughter() d2 = Daughter() session.add(m1) session.add(m2) session.add(d1) session.add(d2) session.commit() m1.daughter = d1 m2.daughter = d2 session.commit() m1.daughter = d2 session.commit() assert m2.daughter is None On Fri, Dec 18, 2020, at 2:01 AM, 'Sören Textor' via sqlalchemy wrote: > Hello > I have a huge problem with süecific "one to one" relation. > > Woking (it's the tutorial code) > > class Son(db.Model): > __tablename__ = 'son' > id = db.Column(db.Integer, primary_key=True) > papa_id = db.Column(db.Integer, db.ForeignKey('papa.id')) > papa = db.relationship("Papa", foreign_keys=[papa_id], > back_populates="son") > > class Papa(db.Model): > __tablename__ = 'papa' > id = db.Column(db.Integer, primary_key=True) > son = db.relationship("Son", uselist=False, back_populates="papa") > > main: > p1 = Papa() > p2 = Papa() > s1 = Son() > s2 = Son() > > db.session.add(p1) > db.session.add(p2) > db.session.add(s1) > db.session.add(s2) > > db.session.commit() > p1.son = s1 > p2.son = s2 > db.session.commit() > p1.son = s2 > db.session.commit() > > Works like a charm. afterwards every relation is correct > > My code (I have to use a super class, that's the only difference): > > class Super(db.Model): > __tablename__ = 'super' > id = db.Column(db.Integer, primary_key=True) > > class Mama(Super): > __tablename__ = 'mama' > id = db.Column(db.Integer, db.ForeignKey('super.id'), primary_key=True) > > daughter_id = db.Column(db.Integer, db.ForeignKey('daughter.id')) > daughter = db.relationship("Daughter", foreign_keys=[daughter_id], > back_populates="mama") > > class Daughter(Super): > __tablename__ = 'daughter' > id = db.Column(db.Integer, db.ForeignKey('super.id'), primary_key=True) > mama = db.relationship("Mama", foreign_keys=[Mama.daughter_id], > uselist=False, back_populates="daughter") > > main: > m1 = Mama() > m2 = Mama() > d1 = Daughter() > d2 = Daughter() > > db.session.add(m1) > db.session.add(m2) > db.session.add(d1) > db.session.add(d2) > db.session.commit() > > m1.daughter = d1 > m2.daughter = d2 > db.session.commit() > > m1.daughter = d2 > db.session.commit() > > everything is correct EXCEPT: > *m2.daughter! *it still points on d2 instead of None. And the table contains > still the daughter_id of d2. > > Thus, what foreign key did I miss? > > All the best and stay healthy! > SirAnn > > > -- > 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 view this discussion on the web visit > https://groups.google.com/d/msgid/sqlalchemy/592197fc-0f15-4c99-a2a7-a9443767bcedn%40googlegroups.com > > <https://groups.google.com/d/msgid/sqlalchemy/592197fc-0f15-4c99-a2a7-a9443767bcedn%40googlegroups.com?utm_medium=email&utm_source=footer>. -- 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 view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/f114a07b-78e9-449b-85be-54210b7ce029%40www.fastmail.com.