ConcurrentModificationError exception

22 views
Skip to first unread message

Mike Conley

unread,
Nov 11, 2009, 11:45:57 AM11/11/09
to sqlal...@googlegroups.com
I am getting this error on a session.flush().

    sqlalchemy.orm.exc.ConcurrentModificationError: Updated rowcount 0 does not match number of objects updated 1

and can't figure out where to start looking.

The database is sqlite, there are no other users connected to the database, and only one process running at the time.

This is happening in the middle of a fairly large routine that is copying a data structure within my database. The routine starts at a high level parent object and copies the parent and many related children through several intertwined relationships resulting in a copy of the parent and all related rows in the database. Think of it as copying object "X" and everything about "X" to a new object "Y" that will have a copy of all "X" ' s information. There are about 20 tables involved in the process. Periodically in the process I need to do a flush() to get the id's of some new objects and it is in one of  these flush() operations that I get this error.

I'm looking for any help I can get about what could cause the error or what I should look at to debug it and track down the cause. I am pretty sure it will be in my code someplace because this is all new code and I am just in the unit testing stage.


--
Mike Conley

Michael Bayer

unread,
Nov 11, 2009, 1:33:35 PM11/11/09
to sqlal...@googlegroups.com
Mike Conley wrote:
> I am getting this error on a session.flush().
>
> sqlalchemy.orm.exc.ConcurrentModificationError: Updated rowcount 0
> does
> not match number of objects updated 1
>
> and can't figure out where to start looking.

it means an UPDATE was issued towards updating the values of a particular
persistent mapped instance, and that the primary key represented by this
instance did not exist in the database at the time the UPDATE was issued,
so no rows matched the WHERE clause.

Since your operation is a "copy", in theory there should only be INSERTs
and not UPDATEs. So at the point of your flush(), session.dirty should
be empty, everything pending would be in session.new. So that's one
place to look. The other is in SQL logging, you'd see an UPDATE statement
immediately before the exception throw which should indicate exactly what
type of object is targeted as well as what the expected primary key value
of that object is.

Mike Conley

unread,
Nov 11, 2009, 2:55:34 PM11/11/09
to sqlal...@googlegroups.com
Thanks, that helps. It looks like the problem is around a M:N relationship. There is some complex logic about whether or not the row on the :N side of the relation should be copied or the original referenced. The related maintenance of the supporting association table causes the update problem.


Reply all
Reply to author
Forward
0 new messages