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 [email protected]
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
-~----------~----~----~----~------~----~------~--~---