The attached script fails with this:
Traceback (most recent call last):
  File "stale_delete.py", line 33, in <module>
    session.flush()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", 
line 1559, in flush
    self._flush(objects)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", 
line 1630, in _flush
    flush_context.execute()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/unitofwork.py", 
line 331, in execute
    rec.execute(self)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/unitofwork.py", 
line 498, in execute
    uow
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/mapper.py", line 
2507, in _delete_obj
    (table.description, len(del_objects), c.rowcount)
sqlalchemy.orm.exc.StaleDataError: DELETE statement on table 'A' expected to 
delete 2 row(s); 1 were matched.


in MySQL-InnoDB (works in SQLite and Postgres).
Tried versions 0.7.3 and 0.7.5
Python 2.7

I tried manually deleting the rows from the table through MySQL client and 
noticed that it's not returning an accurate row count (doesn't 
'supports_sane_rowcount') for a table with an adjacency relationship and an 
ondelete='CASCADE'.

I'm going to work around this by adding another ondelete='CASCADE' for User->A
-- 
Fayaz Yusuf Khan
Cloud developer and architect
Dexetra SS, Bangalore, India
fayaz.yusuf.khan_AT_gmail_DOT_com
fayaz_AT_dexetra_DOT_com
+91-9746-830-823
from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship, backref


Base = declarative_base(bind=create_engine('mysql://root@localhost/test'))


class User(Base):

    __tablename__ = 'User'
    id = Column(Integer, primary_key=True)


class A(Base):

    __tablename__ = 'A'
    id = Column(Integer, primary_key=True)
    user_id = Column(Integer, ForeignKey(User.id))
    user = relationship(User, backref=backref(
            'a', cascade='all, delete, delete-orphan'))
    parent_id = Column(Integer, ForeignKey('A.id', ondelete='CASCADE'))


Base.metadata.create_all()
session = sessionmaker()()
user = User()
session.add(user)
session.add_all([A(id=1, parent_id=1, user=user),
                 A(id=2, parent_id=1, user=user)])
session.flush()
session.delete(user)
session.flush()

Attachment: signature.asc
Description: This is a digitally signed message part.

Reply via email to