On 2/14/2014 1:51 PM, Michael Bayer wrote:
right this is why reading the docs is better, those have been checked...  
remote side for m2o refers to the primary key, so:

The docs says: '...directive is added known as remote_side, which is a Column or collection of Column objects that indicate those which should be considered to be "remote":'

Nothing there particularly hints that it must be the primary key.


from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class Animal(Base):
     __tablename__ = 'animals'
     id_ = Column(Integer, primary_key=True)

     sire_id = Column(Integer, ForeignKey('animals.id_'))
     dam_id = Column(Integer, ForeignKey('animals.id_'))

     sire = relationship('Animal', foreign_keys=[sire_id], remote_side=id_)
     dam = relationship('Animal', foreign_keys=[dam_id], remote_side=id_)
     pjoin = 'and_(Animal.sire_id == Animal.id_, Animal.dam_id == Animal.id_)'
     children = relationship('Animal', primaryjoin=pjoin)

e = create_engine("sqlite://", echo=True)
Base.metadata.create_all(e)

s = Session(e)

a1, a2, a3, a4 = Animal(), Animal(), Animal(), Animal()

a1.sire = a3

This is working great until I add in this line:
  a1.sire = a1
Gives exception:

sqlalchemy.exc.CircularDependencyError: Circular dependency detected. Cycles: set([ProcessState(ManyToOneDP(Animal.sire), <Animal at 0x33f13b0>, delete=False), SaveUpdateState(<Animal at 0x33f13b0>)]) all edges: set([(SaveUpdateState(<Animal at 0x348b970>), ProcessState(ManyToOneDP(Animal.dam), <Animal at 0x33fa430>, delete=False)), (SaveUpdateState(<Animal at 0x33f13b0>), ProcessState(ManyToOneDP(Animal.sire), <Animal at 0x33f13b0>, delete=False))
...

No bull ever sired himself. But the first object I intended to populate in the db would be the "unknown" sire who would just have unknown (himself) as a sire. The 'sire' column needs to be not nullable or I'd just leave it blank.

Any suggestions?



also reading the terms here, what's "children" supposed to be?   wouldn't that "AND" be 
an "OR" here if I understand correctly?

Yes, quite right.

Thanks for your kind help,
Michael


--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to