It's not a self-referring relation : Child and Parent objects are different types and tables. So "remote_side" won't help here I fear.
And I can't really specify the foreign key on the column in my special case (that's what I do usually of course) In a single inheritance pattern : - my top level class (the one that creates the table) has no foreign key at all, but has a 'id_foreign' column, used by its subclasses to have relation with any types of mapped objects - any subclass may have its own relation with a lambda object : that's why I use foreign_keys and primaryjoin. The above code is just a simple code that reproduce what happen in my complex situation... Maybe it is a silly idea to do that, and I should use multi-table inheritance instead... On 31 oct, 01:01, Michael Bayer <[EMAIL PROTECTED]> wrote: > the many-to-one side of a self-referring relation() needs the > "remote_side" argument to be set; in this case it would be on the > relation(Parent, remote_side=Parent.id). > > Also if you specify a ForeignKey() on your Child.parent_id Column > object, you won't need the "primaryjoin" or "foreign_keys" argument in > either the relation() or the backef(). This is SQLA's more typical > configurational style. > > On Oct 30, 2008, at 6:58 PM, GustaV wrote: > > > > > Hi everybody. > > > I'm stuck in something bad: > > I use a relation with a arbitrary foreign key + backref (the reason is > > that I use single table inheritance, and the relation happen only on > > one of the subclasses, so I can't set the foreign key on the > > table...). But I get quickly a circular dependency! > > > I don't really get why, so I wrote a piece of code to reproduce it and > > post it here! > > I hope someone got an explanation/solution! :) > > > Cheers! > > ps: using the latest revision. > > pps : I put the relation on the child side, but the same happens when > > on the parent side. > > > from zope.sqlalchemy import ZopeTransactionExtension > > from sqlalchemy.orm import scoped_session, sessionmaker, eagerload > > from sqlalchemy import MetaData > > from sqlalchemy.orm.interfaces import SessionExtension > > from sqlalchemy.ext.declarative import declarative_base > > from sqlalchemy import Table, Column, types > > from sqlalchemy import ForeignKey, UniqueConstraint > > from sqlalchemy.orm import relation, backref, synonym > > from sqlalchemy import select, func > > > maker = sessionmaker(autoflush=True, autocommit=False, > > extension=[ZopeTransactionExtension()]) > > DBSession = scoped_session(maker) > > DeclarativeBase = declarative_base() > > > class Parent(DeclarativeBase): > > __tablename__ = 'parent' > > id = Column('id', types.Integer, primary_key=True, > > nullable=False) > > > class Child(DeclarativeBase): > > __tablename__ = 'child' > > id = Column('id', types.Integer, primary_key=True, > > nullable=False) > > id_parent = Column('id_parent', types.Integer, > > nullable=True) > > parent = relation(Parent, > > primaryjoin=id_parent==Parent.id, > > foreign_keys=[Parent.id], > > backref=backref('children', > > foreign_keys=[id_parent], > > cascade='all, delete-orphan', > > passive_deletes=False)) > > > p = Parent() > > c = Child() > > p.children.append(c) > > DBSession.add(p) > > DBSession.flush() --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---