Skip to content

Commit

Permalink
fix upsert id
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Jun 4, 2024
1 parent 654c150 commit f9d5cd8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 1 addition & 1 deletion fastlite/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2890,7 +2890,7 @@ def insert_chunk(

else:
raise
if num_records_processed == 1:
if num_records_processed == 1 and result.lastrowid:
self.last_rowid = result.lastrowid
self.last_pk = self.last_rowid
# self.last_rowid will be 0 if a "INSERT OR IGNORE" happened
Expand Down
4 changes: 3 additions & 1 deletion fastlite/kw.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def xtra(self:Table, **kwargs):

@patch
def get_last(self:Table, as_cls:bool=True):
assert self.last_rowid
row = first(self.rows_where('_rowid_=?', (self.last_rowid,)))
assert row, f"Couldn't find {self.last_rowid}"
vals = [row[pk] for pk in self.pks]
Expand Down Expand Up @@ -200,11 +201,12 @@ def upsert(
if not record: record={}
if is_dataclass(record): record = asdict(record)
record = {**record, **kwargs}
last_pk = record[pk]
self._orig_upsert(
record=record, pk=pk, foreign_keys=foreign_keys, column_order=column_order, not_null=not_null,
defaults=defaults, hash_id=hash_id, hash_id_columns=hash_id_columns, alter=alter,
extracts=extracts, conversions=conversions, columns=columns, strict=strict)
return self.get_last()
return self.get(last_pk)


@patch
Expand Down

0 comments on commit f9d5cd8

Please sign in to comment.