Skip to content

Commit

Permalink
Proper last insert ID handling for apsw extension w/RETURNING.
Browse files Browse the repository at this point in the history
Fixes #2479
  • Loading branch information
coleifer committed Oct 27, 2021
1 parent 540dbf6 commit 9e90bea
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ https://github.com/coleifer/peewee/releases

## master

* Fix bug in APSW extension with Sqlite 3.35 and newer, due to handling of last
insert rowid with RETURNING. Refs #2479.

[View commits](https://github.com/coleifer/peewee/compare/3.14.6...master)

## 3.14.6
Expand Down
10 changes: 9 additions & 1 deletion playhouse/apsw_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from peewee import DateField as _DateField
from peewee import DateTimeField as _DateTimeField
from peewee import DecimalField as _DecimalField
from peewee import Insert
from peewee import TimeField as _TimeField
from peewee import logger

Expand Down Expand Up @@ -91,7 +92,14 @@ def load_extension(self, extension):
conn.loadextension(extension)

def last_insert_id(self, cursor, query_type=None):
return cursor.getconnection().last_insert_rowid()
if not self.returning_clause:
return cursor.getconnection().last_insert_rowid()
elif query_type == Insert.SIMPLE:
try:
return cursor[0][0]
except (AttributeError, IndexError, TypeError):
return cursor.getconnection().last_insert_rowid()
return cursor

def rows_affected(self, cursor):
return cursor.getconnection().changes()
Expand Down

0 comments on commit 9e90bea

Please sign in to comment.