-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Comments
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. |
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.
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 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() |
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.
I would expect the
Model.insert_many().execute()
return value to therowcount
affected, however it appears to always be0
(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:Any suggestions for how this is better implemented? I am using MySQL 5.6, although I'm not sure that matters.
The text was updated successfully, but these errors were encountered: