On 4/14/15 6:38 AM, Steven Winfield wrote:
Hi,

I think I've found a bug triggered by bulk deletes that use the (default) "evaluate" strategy.

a bug is created at https://bitbucket.org/zzzeek/sqlalchemy/issue/3365/evaluator-cant-locate-orm-entity-when and for now you need to compare using the columns, not the relationship, e.g. Child._id_parent == parent.id, if you want to use evaluate there.



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:%5Cuser%21winfis%21test_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:%5CR:%5Csw%5Cexternal%5C20150407-0%5Cpython27%5Clib%5Csite-packages%5Csqlalchemy-0.9.7-py2.7-win32.egg%5Csqlalchemy%5Corm%5Cquery.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:%5CR:%5Csw%5Cexternal%5C20150407-0%5Cpython27%5Clib%5Csite-packages%5Csqlalchemy-0.9.7-py2.7-win32.egg%5Csqlalchemy%5Corm%5Cpersistence.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:%5CR:%5Csw%5Cexternal%5C20150407-0%5Cpython27%5Clib%5Csite-packages%5Csqlalchemy-0.9.7-py2.7-win32.egg%5Csqlalchemy%5Corm%5Cpersistence.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:%5CR:%5Csw%5Cexternal%5C20150407-0%5Cpython27%5Clib%5Csite-packages%5Csqlalchemy-0.9.7-py2.7-win32.egg%5Csqlalchemy%5Corm%5Cevaluator.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:%5CR:%5Csw%5Cexternal%5C20150407-0%5Cpython27%5Clib%5Csite-packages%5Csqlalchemy-0.9.7-py2.7-win32.egg%5Csqlalchemy%5Corm%5Cevaluator.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] <mailto:[email protected]>. To post to this group, send email to [email protected] <mailto:[email protected]>.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
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.

Reply via email to