Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How best to get inserted row count from Model.insert_many()? #2026

Closed
grantcox opened this issue Sep 27, 2019 · 3 comments
Closed

How best to get inserted row count from Model.insert_many()? #2026

grantcox opened this issue Sep 27, 2019 · 3 comments

Comments

@grantcox
Copy link

I would expect the Model.insert_many().execute() return value to the rowcount affected, however it appears to always be 0 (presumably it's attempting to get the primary row id inserted?).

I have found that the following approach works, hacking on how the returning implementation works. But it doesn't look like the correct way:

multi_insert_query = MyModel.insert_many(rows).on_conflict(action='IGNORE')
multi_insert_query._return_cursor = True
cursor = multi_insert_query.execute()
total_inserted = cursor.rowcount

Any suggestions for how this is better implemented? I am using MySQL 5.6, although I'm not sure that matters.

@coleifer
Copy link
Owner

I hear you -- thanks for mentioning the database you're using. This should work with sqlite as well, though I don't believe it will work for postgres -- but I need to test it out. Let me look into this as I think it's a worthwhile request.

@coleifer
Copy link
Owner

ab43376

coleifer added a commit that referenced this issue Mar 28, 2022
This change is backwards incompatible and affects the default value
returned by bulk insert operations on MySQL and Sqlite (when no
RETURNING clause is specified).

Prior to this commit, Peewee has (since 3.12) returned the modified
row-count rather than the cursor lastrowid. This change returns Peewee
to its previous behavior of returning the lastrowid, and exposes the
modified rowcount using a new `as_rowcount()` chainable method on the
Insert query object.
@coleifer
Copy link
Owner

Just a heads-up that this change has been rolled-back in #2556. Going forwards, to obtain the inserted row count, you may use the Insert.as_rowcount() chainable query method. Examples:

db = MySQLDatabase(...)

query = User.insert_many([...])
# Old behavior:
#rowcount = query.execute()

# Behavior going forwards:
lastid = query.execute()

# To get the rowcount instead of the last id:
rowcount = query.as_rowcount().execute()

coleifer added a commit that referenced this issue Mar 28, 2022
This change is backwards incompatible and affects the default value
returned by bulk insert operations on MySQL and Sqlite (when no
RETURNING clause is specified).

Prior to this commit, Peewee has (since 3.12) returned the modified
row-count rather than the cursor lastrowid. This change returns Peewee
to its previous behavior of returning the lastrowid, and exposes the
modified rowcount using a new `as_rowcount()` chainable method on the
Insert query object.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants