Skip to content

Commit

Permalink
Docs clarification and test cleanup.
Browse files Browse the repository at this point in the history
Fixes #2876
  • Loading branch information
coleifer committed Apr 22, 2024
1 parent 1191506 commit e1153d8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
21 changes: 20 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ class MyModel(db.Model):

Rollback behavior change in commit ab43376697 (GH #2026). Peewee will no longer
automatically return the cursor `rowcount` for certain bulk-inserts. This
should only affect users of MySQL and Sqlite who relied on a bulk INSERT
should mainly affect users of MySQL and Sqlite who relied on a bulk INSERT
returning the `rowcount` (as opposed to the cursor's `lastrowid`). The
`rowcount` behavior is still available chaining the ``as_rowcount()`` method:

Expand All @@ -233,6 +233,25 @@ last_id = query.execute()
rows_inserted = query.as_rowcount().execute()
```

Additionally, in previous versions specifying an empty `.returning()` with
Postgres would cause the rowcount to be returned. For Postgres users who wish
to receive the rowcount:

```python
# NOTE: this change only affects Postgresql.
db = PostgresqlDatabase(...)

# Previously, an empty returning() would return the rowcount.
query = User.insert_many(...) # Bulk insert.
query = User.insert_from(...) # Bulk insert (INSERT INTO .. SELECT FROM).

# Old behavior:
# rows_inserted = query.returning().execute()

# To get the rows inserted in 3.15 and newer:
rows_inserted = query.as_rowcount().execute()
```

This release contains a fix for a long-standing request to allow data-modifying
queries to support CTEs. CTEs are now supported for use with INSERT, DELETE and
UPDATE queries - see #2152.
Expand Down
4 changes: 0 additions & 4 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,6 @@ def test_insert_rowcount(self):
User.create(username='u0') # Ensure that last insert ID != rowcount.

iq = User.insert_many([(u,) for u in ('u1', 'u2', 'u3')])
if IS_POSTGRESQL or IS_CRDB:
iq = iq.returning()
self.assertEqual(iq.as_rowcount().execute(), 3)

# Now explicitly specify empty returning() for all DBs.
Expand All @@ -433,8 +431,6 @@ def test_insert_rowcount(self):
.select(User.username.concat('-x'))
.where(User.username.in_(['u1', 'u2'])))
iq = User.insert_from(query, ['username'])
if IS_POSTGRESQL or IS_CRDB:
iq = iq.returning()
self.assertEqual(iq.as_rowcount().execute(), 2)

query = (User
Expand Down

0 comments on commit e1153d8

Please sign in to comment.