On Feb 14, 2014, at 12:46 PM, Michael Hipp <mich...@redmule.com> wrote:

> On 2/13/2014 11:45 AM, Michael Bayer wrote:
>> So for "children" above you need to spell out primaryjoin completely which 
>> is primaryjoin="and_(Animal.sire_id == Animal.id_, Animal.dam_id == 
>> Animal.id)". 
> 
> Thought I was on the right track but now getting the exception below. Here's 
> the model:
> 
> 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])
>    dam = relationship('Animal', foreign_keys=[dam_id])
>    pjoin = 'and_(Animal.sire_id == Animal.id_, Animal.dam_id == Animal.id_)'
>    children = relationship('Animal', foreign_keys=[sire_id, dam_id],
>                            primaryjoin=pjoin)
> 
> So I attempt to put in the first object, which is to be a bit special:
> 
>    unknown = Animal(id_=0)
>    db_session.add(unknown)
>    unknown.sire = unknown   # <- get exception here
>    unknown.dam = unknown
>    db_session.commit()
> 
> TypeError: Incompatible collection type: Animal is not list-like
> 
> unknown.sire shows to contain [] so it evidently wants a list of sires? 
> That's not what I had in mind for the above model.  Any help?

well here we’re doing a self-referential relationship so in order to make a 
many-to-one self ref, as someone else mentioned you need remote_side=sire_id

background:

http://docs.sqlalchemy.org/en/rel_0_9/orm/relationships.html#adjacency-list-relationships


Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to