sqlite-utils icon indicating copy to clipboard operation
sqlite-utils copied to clipboard

.delete_where() does not auto-commit (unlike .insert() or .upsert())

Open spdkils opened this issue 5 years ago • 10 comments

When you use the delete_where() function on a table, it never commits....

Is that intentional?

spdkils avatar Sep 16 '20 01:09 spdkils

See #121 - I need to think harder about how this all interacts with transactions.

You can do this:

with db.conn:
    db["mytable"].delete_where()

But that should be documented and maybe rethought.

simonw avatar Sep 16 '20 06:09 simonw

I appreciate the response, it's just unexpected.

If I insert, it commits, if I update it commits, if I upsert it commits... if I delete.. it doesn't??? Confused me...

I did just db commit it... But it's confusing.

spdkils avatar Sep 16 '20 15:09 spdkils

Yeah I'm going to class this as a bug - that's definitely confusing.

simonw avatar Sep 16 '20 18:09 simonw

Independent of the transaction changes in #121 I may be able to check self.conn.in_transaction to see if a transaction is active and, if one is NOT active, execute the delete inside of one. https://docs.python.org/3/library/sqlite3.html#sqlite3.Connection.in_transaction

simonw avatar Sep 16 '20 22:09 simonw

That's strange... this test here doesn't manually commit a transaction and passes:

https://github.com/simonw/sqlite-utils/blob/7805d53bcf11199bd1f2b07e05ae90151f9d0eb0/tests/test_delete.py#L17-L23

simonw avatar Sep 16 '20 22:09 simonw

@spdkils can you share a minimal code example that exhibits the behavior you're seeing?

simonw avatar Sep 16 '20 22:09 simonw

I confirm the bug. Happens for me in version 3.6. I use the call to delete all the records: table.delete_where() This does not delete anything.

I see that delete() method DOES use context manager with self.db.conn: which should help. You may want to align the code of both methods.

limar avatar Mar 18 '21 15:03 limar

Just stumbled across this, wondering why none of my deletes were working.

dracos avatar Apr 27 '22 21:04 dracos

In a related issue, nearly a year later I just stumbled across this again, as I wondered why none of my rebuild-fts were rebuilding. It looks like: disable_fts in db.py commits; enable_fts partly commits except the last step (due to executescript committing a pending transaction); rebuild_fts won't commit unless manually done as above with e.g. a context manager.

dracos avatar Apr 01 '23 17:04 dracos