Hi there, I'm experimenting with a composite primary key to see whether it might help implement a workflow system, where the primary key consists of an identifier (code) and a workflow status.
I run into an error with the ORM (in 0.5.5 and trunk) when I modify part of the primary key (the workflow status). It looks like the session somehow retains a reference to something that isn't around anymore. The documentation claims primary keys can be mutated, but perhaps I'm doing something that really shouldn't be done after all? I've attached the code to reproduce the issue (reproduce.py). The error is during the commit on the last line, when the primary keys of two items are modified. Here is the traceback: Traceback (most recent call last): File "bin/devpython", line 25, in ? execfile(sys.argv[0]) File "reproduce.py", line 44, in ? session.commit() File ".../sqlalchemy/orm/session.py", line 673, in commit self.transaction.commit() File ".../sqlalchemy/orm/session.py", line 378, in commit self._prepare_impl() File ".../sqlalchemy/orm/session.py", line 362, in _prepare_impl self.session.flush() File ".../sqlalchemy/orm/session.py", line 1358, in flush self._flush(objects) File ".../sqlalchemy/orm/session.py", line 1445, in _flush flush_context.finalize_flush_changes() File ".../sqlalchemy/orm/unitofwork.py", line 288, in finalize_flush_changes self.session._register_newly_persistent(elem.state) File ".../sqlalchemy/orm/session.py", line 1021, in _register_newly_persistent self.identity_map.remove(state) File ".../sqlalchemy/orm/identity.py", line 135, in remove raise AssertionError("State %s is not present in this identity map" % state) AssertionError: State <sqlalchemy.orm.state.InstanceState object at 0xb769ae4c> is not present in this identity map Regards, Martijn --~--~---------~--~----~------------~-------~--~----~ 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 sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---
from sqlalchemy import create_engine, MetaData from sqlalchemy import Table, Column, Integer, Unicode from sqlalchemy.orm import sessionmaker, mapper NEW = 0 PUBLISHED = 1 EDITABLE = 2 ARCHIVED = 3 engine = create_engine('sqlite:///:memory:') metadata = MetaData() Session = sessionmaker(bind=engine) session = Session() user = Table( 'user', metadata, Column('code', Integer, primary_key=True), Column('status', Integer, primary_key=True), Column('username', Unicode, nullable=False), ) metadata.create_all(engine) class User(object): def __init__(self, code, status, username): self.code = code self.status = status self.username = username mapper(User, user) a_published = User(0, PUBLISHED, u'a') session.add(a_published) session.commit() a_editable = User(0, EDITABLE, u'a') session.add(a_editable) session.commit() a_published.status = ARCHIVED a_editable.status = PUBLISHED session.commit()