I am getting an problem where I recieve a a mistake for example that one:

"MyPyramidApplication Error"<class 'sqlalchemy.orm.exc.StaleDataError'>: DELETE statement on table 'page_view' expected to delete 6 row(s); Only 0 were matched.

So, I've got a wise decision what's leading to the problem but I've been not able to resolve it.

I've got a page_view model, which has a foreign key on page_id along with a user_id.

This is what the model appears like:

page_view_table = sa.Table(
    sa.Column('id', sa.Integer, primary_key=True),
    sa.Column('page_id', sa.Integer, sa.ForeignKey('guide.id')),
    sa.Column('user_id', sa.Integer, sa.ForeignKey('user.id')),
    sa.Column('last_view', sa.DateTime, nullable=False),
    sa.UniqueConstraint('user_id', 'page_id'),

This is what the relations seem like

orm.mapper(Page, page_table,
    properties = {
        'users_viewed': sa.orm.relation(

I'm adding some products to my database utilizing an place statement, something such as this:

ins = model.page_view_table.insert()
sql = str(ins)
sql += ' ON DUPLICATE KEY UPDATE last_view = :last_view'
session = model.Session()
session.execute(sql, page_views)

So far as I will tell in the logs, the transactions will get committed correctly and that i begin to see the products within the DB.

However, after i attempt to remove the page item while using ORM, I recieve the StaleDataError exception. Searching in the logs, I begin to see the ORM giving a remove statement however moving back because of the mistake.

I've attempted experimentation with session.expire_all() in addition to session.expunge_all() immediately after the place statement however they were not very useful and that i still the mistake.

This is what within the SQLAlchemy logs.

2011-11-05 18:06:08,031 INFO  [sqlalchemy.engine.base.Engine][worker 3] DELETE FROM page_view WHERE page_view.page_id = %s AND page_view.user_id = %s
2011-11-05 18:06:08,031 INFO  [sqlalchemy.engine.base.Engine][worker 3] (13818L, 259L)
2011-11-05 18:06:08,032 INFO  [sqlalchemy.engine.base.Engine][worker 3] DELETE FROM page_view WHERE page_view.page_id = %s AND page_view.user_id = %s
2011-11-05 18:06:08,033 INFO  [sqlalchemy.engine.base.Engine][worker 3] (13818L, 259L)
2011-11-05 18:06:08,033 INFO  [sqlalchemy.engine.base.Engine][worker 3] ROLLBACK

I figured the double remove statement was a suspect, maybe pointing to some misconfigured ORM relation however i don't believe this is the situation.