Weird behavior of `delete` using different query param type

Issue #3570 invalid
Shipeng Feng created an issue
>>> t
<Text 643544210980470784>
>>> print t.__dict__
{'content': u'ha', 'created_ts': 1446212896L, '_sa_instance_state': <sqlalchemy.orm.state.InstanceState object at 0x100c615d0>, 'updated_ts': 1446212896L, 'id': 643544210980470784L}
>>> models.Text.query.filter_by(id='643544210980470784').delete()
1L
>>> session.commit()
>>> print t.__dict__
{'_sa_instance_state': <sqlalchemy.orm.state.InstanceState object at 0x100c615d0>}

The t's attribute can not be accessed any more, I try it again with id as integer, and get different result:

>>> t
<Text 643544531031031808>
>>> print t.__dict__
{'content': u'ah', 'created_ts': 1446212972L, '_sa_instance_state': <sqlalchemy.orm.state.InstanceState object at 0x108ca45d0>, 'updated_ts': 1446212972L, 'id': 643544531031031808L}
>>> models.Text.query.filter_by(id=643544531031031808).delete()
1L
>>> session.commit()
>>> print t.__dict__
{'content': u'ah', 'created_ts': 1446212972L, '_sa_instance_state': <sqlalchemy.orm.state.InstanceState object at 0x108ca45d0>, 'updated_ts': 1446212972L, 'id': 643544531031031808L}

Comments (4)

  1. Mike Bayer repo owner

    so there's no bug here. In both cases, the DELETE is accepted by your database, and the row is deleted. The delete() here is called with the default synchronize_session of "evaluate", meaning the ORM will attempt to locate these objects in the Session as well and handle them accordingly. However, in the first case, you're using the wrong data type, the ORM does not locate your "t" object since Python comparisons are strongly typed, and then when you call commit(), the object is expired. In the second case, the ORM locates your known-to-be-deleted object, removes it from the Session, and it is not subject to the expiration operation when commit() is called.

    please use the mailing list for questions like these, if an actual bug is located we'll create it here thanks!

  2. Log in to comment