Heres a test case which works fine: from sqlalchemy import * from sqlalchemy.orm import *
engine = create_engine('sqlite://', echo=True) meta = MetaData(engine) r_table = Table('relation', meta, Column('id', Integer, primary_key=True, autoincrement=True), Column('left', String(length=32), ForeignKey("soup.uuid"), index=True), Column('right', String(length=32),ForeignKey("soup.uuid")), ) s_table = Table('soup', meta, Column('uuid', String(50), primary_key=True)) meta.create_all() class S(object): def __init__(self, uuid): self.uuid = uuid class R(object): def __init__(self, left, right): self.left = left self.right = right mapper(S, s_table) mapper(R, r_table, properties={ '_left':r_table.c.left, '_right':r_table.c.right, 'left':relation(S, primaryjoin=s_table.c.uuid==r_table.c.left), 'right':relation(S, primaryjoin=s_table.c.uuid==r_table.c.right), }) sess = create_session() s1 = S('one') s2 = S('two') r1 = R(s1, s2) sess.save(r1) sess.flush() assert r1._left == s1.uuid assert r1._right == s2.uuid assert r_table.select().execute().fetchall() == [(1, 'one', 'two')] On Jun 10, 2008, at 10:34 AM, Malthe Borch wrote: > > I have an issue with SQLAlchemy planning to execute insertion tasks in > the wrong order. > > Basically, I have a utility table "Relations" which is used to > maintain > ordered list relations: > > table = rdb.Table( > 'relation', > metadata, > rdb.Column('id', rdb.Integer, primary_key=True, autoincrement=True), > rdb.Column('left', rdb.String(length=32), > rdb.ForeignKey("soup.uuid"), index=True), > rdb.Column('right', rdb.String(length=32), > rdb.ForeignKey("soup.uuid")), > rdb.Column('order', rdb.Integer, nullable=False)) > > Now, I append a new, transient object to such an ordered list. That > means that SQLAlchemy would make two inserts. The problem is that the > tasks are ordered such that the *relation* is inserted before the > object > that is the target of the relation! > > This obviously raises an IntegrityError, since the foreign key > constraint is not satisfied. > > My question is then: How do I tell SQLAlchemy to order them correctly? > > \malthe > > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---