Hi,
I think I've found a bug triggered by bulk deletes that use the (default)
"evaluate" strategy.
For example:
from sqlalchemy import Column, Integer, Text, ForeignKey, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship
Base = declarative_base()
class Parent(Base):
__tablename__ = "parent"
id = Column(Integer, primary_key=True)
class Child(Base):
__tablename__ = "child"
_id_parent = Column("id_parent", Integer, ForeignKey(Parent.id),
primary_key=True)
name = Column(Text, primary_key=True)
parent = relationship(Parent)
engine = create_engine('sqlite://')
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
session.bind.echo = True
# Make a parent
p = Parent(id=1)
session.add(p)
session.commit()
# Add a child
c = Child(name="foo", parent=p)
session.add(c)
session.commit()
# c is still in the session
session.query(Child).filter(Child.parent == p).delete("evaluate")
...give the following traceback:
File user!winfis!test_bed.py, line 34, in :
session.query(Child).filter(Child.parent == p).delete("evaluate")
<file:\user!winfis!test_bed.py:34:exception>
File
R:\sw\external\20150407-0\python27\lib\site-packages\sqlalchemy-0.9.7-py2.7-win32.egg\sqlalchemy\orm\query.py,
line 2670, in delete : delete_op.exec_()
<file:\R:\sw\external\20150407-0\python27\lib\site-packages\sqlalchemy-0.9.7-py2.7-win32.egg\sqlalchemy\orm\query.py:2670:exception>
File
R:\sw\external\20150407-0\python27\lib\site-packages\sqlalchemy-0.9.7-py2.7-win32.egg\sqlalchemy\orm\persistence.py,
line 896, in exec_ : self._do_pre_synchronize()
<file:\R:\sw\external\20150407-0\python27\lib\site-packages\sqlalchemy-0.9.7-py2.7-win32.egg\sqlalchemy\orm\persistence.py:896:exception>
File
R:\sw\external\20150407-0\python27\lib\site-packages\sqlalchemy-0.9.7-py2.7-win32.egg\sqlalchemy\orm\persistence.py,
line 958, in _do_pre_synchronize : eval_condition(obj)]
<file:\R:\sw\external\20150407-0\python27\lib\site-packages\sqlalchemy-0.9.7-py2.7-win32.egg\sqlalchemy\orm\persistence.py:958:exception>
File
R:\sw\external\20150407-0\python27\lib\site-packages\sqlalchemy-0.9.7-py2.7-win32.egg\sqlalchemy\orm\evaluator.py,
line 116, in evaluate : right_val = eval_right(obj)
<file:\R:\sw\external\20150407-0\python27\lib\site-packages\sqlalchemy-0.9.7-py2.7-win32.egg\sqlalchemy\orm\evaluator.py:116:exception>
File
R:\sw\external\20150407-0\python27\lib\site-packages\sqlalchemy-0.9.7-py2.7-win32.egg\sqlalchemy\orm\evaluator.py,
line 72, in : return lambda obj: get_corresponding_attr(obj)
<file:\R:\sw\external\20150407-0\python27\lib\site-packages\sqlalchemy-0.9.7-py2.7-win32.egg\sqlalchemy\orm\evaluator.py:72:exception>
AttributeError: 'Child' object has no attribute 'id_parent'
...because the attribute lookup on Child is attempted using the column
name, rather than the attribute name. The actual delete action works fine
when I switch the strategy to False or "fetch", or if there are no Child
objects in the session (so no evaluation is invoked).
As you can see, this is v0.9.7, but I've not seen anything relevant in the
changelog since then.
Cheers,
Steve.
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.