Well, I thought it was transient. If you refer to the code in the first post: If I create a new Resume object and save() it , it works. If before the save(), I fetch a Workplace object from the DB, then save () fails and I need to use save_and_update().
So the fact that I queried the DB for a related object, makes the Resume object not transient anymore? On Jun 3, 8:56 pm, "Michael Bayer" <[email protected]> wrote: > Moshe C. wrote: > > > Thanks. > > Given that I am not going to upgrade very soon, is it right to > > conclude that there was a bug in 0.4.6, or is my usage wrong? > > it is not a bug. save() is used only for transient instances. > > > > > On Jun 3, 5:28 pm, "Michael Bayer" <[email protected]> wrote: > >> Moshe C. wrote: > > >> > Trying to find out if I hit a bug or it is me doing something wrong. > >> > Using version 0.4.6 > > >> > when creating an object and then calling session.save() I get: > >> > Instance 'res...@-0x486e4074' is already persistent > > >> > It works save_or_update() with, but I don't see why I should use that. > > >> > I did read that using session.mapper can cause this but I use > >> > orm.mapper. > > >> session.save() is only used to persist transient instances. It is > >> deprecated (as is update() and save_or_update()) and not present in > >> version 0.5. Upgrade to at least 0.4.8 if not 0.5 and use > >> session.add() > >> (equivalent to save_or_update()), which eliminates the need for the user > >> to distinguish between transient and detached instances. For a > >> description of what the heck im talking about when i say "transient" and > >> "detached" > >> seehttp://www.sqlalchemy.org/docs/05/session.html#quickie-intro-to-objec... > >> . > > >> > Here is the mapping code: > > >> > metadata = sa.MetaData() > >> > sm = orm.sessionmaker(autoflush=True, transactional=True, > >> > bind=engine) > > >> > Model.session = orm.scoped_session(sm) > > >> > person_table = sa.Table('person', metadata, autoload = True, > >> > autoload_with=engine) > >> > person_relative_table = sa.Table('person_relative', metadata, > >> > autoload = True, autoload_with=engine) > >> > resume_table = sa.Table('resume', metadata, autoload = True, > >> > autoload_with=engine) > >> > workplace_table = sa.Table('workplace', metadata, autoload = > >> > True, autoload_with=engine) > >> > resume_workplace_table = sa.Table('resume_workplace', > >> > metadata, autoload = True, autoload_with=engine) > > >> > orm.mapper(self.Person, person_table, properties = { > >> > 'relatives' : orm.relation(self.Person, > >> > secondary=person_relative_table, > > >> > primaryjoin=person_table.c.id==person_relative_table.c.person_id, > > >> > secondaryjoin=person_relative_table.c.relative_id==person_table.c.id, > >> > backref='followers'), > >> > 'resumes' : orm.relation(self.Resume, backref='person') > >> > } > >> > ) > >> > orm.mapper(self.Resume, resume_table, properties = { > >> > 'workplaces' : orm.relation(self.Workplace, > >> > secondary=resume_workplace_table, backref='resumes') > >> > } > >> > ) > >> > orm.mapper(self.Workplace, workplace_table) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
