Hi, I'm stuck with the code below raise FlushError complaining:
Traceback (most recent call last): File "<stdin>", line 49, in <module> File "/misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/orm/session.py", line 673, in commit self.transaction.commit() File "/misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/orm/session.py", line 378, in commit self._prepare_impl() File "/misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/orm/session.py", line 362, in _prepare_impl self.session.flush() File "/misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/orm/session.py", line 1356, in flush self._flush(objects) File "/misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/orm/session.py", line 1416, in _flush mapperutil.state_str(state), path)) sqlalchemy.orm.exc.FlushError: Instance <Delivery at 0x85cb82c> is an unsaved, pending instance and is an orphan (is not attached to any parent 'Project' instance via that classes' 'deliveries' attribute) I don't understand what I should do to fix it. What I can't understand is why it is considered an orphan: I *do* set job_id on it and it should be enought to build the relation (I *do* need delete-orphan). Is the only solution to attach an instance (u.job = myjob) or is there another solution that doesn't require me to build the instance? Thanks in advance sandro *:-) -- Sandro Dentella *:-) http://sqlkit.argolinux.org SQLkit home page - PyGTK/python/sqlalchemy ------------------------------------------------------------------------ import datetime from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Table, Column, ForeignKey, text, func from sqlalchemy.types import * from sqlalchemy import orm Base = declarative_base() Base.metadata.bind = 'sqlite://' #Base.metadata.bind = 'postgres://localhost/fossati' Session = orm.sessionmaker(bind=Base.metadata.bind) sess = Session() class Project(Base): __tablename__ = "project" id = Column(Integer, primary_key=True) name = Column(String(30), nullable=False) def __str__(self): return self.name class Delivery(Base): __tablename__ = 'delivery' id = Column(Integer, primary_key=True) data = Column(Date, nullable=True) job_id = Column(ForeignKey(Project.id), nullable=False) note = Column(Text) job = orm.relation(Project, backref=orm.backref('deliveries', cascade="all, delete-orphan")) # job = orm.relation(Project, backref='deliveries', lazy=False) def __repr__(self): return self.note __str__ = __repr__ Base.metadata.create_all() Base.metadata.bind.echo=True p = Project(name='test 1') sess.add(p) sess.commit() print "DELIVERY" u = Delivery() u.job_id = p.id # u.job = p ## this way it works u.data = datetime.date(2009, 12, 3) sess.add(u) sess.commit() -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.