Insert from_select Mysql returns number of rows affected, instead of lastrowid/pk
See original GitHub issueUsing an insert_from with mysql will always return the number of rows inserted, instead of returning the lastrowid. Postgresql instead will return a result cursor from which you can derive the lastrowid. So if we use the insert_from style with MySQL there is no way to obtain the lastrowid using the public API.
In Mysql:
# this will correctly return the lastrowid/pk:
Users.insert(name='foo').execute()
# this will always return 1, instead of the lastrowid/pk.
Users.insert_from(
Select(columns=['a']),
['name']
).execute()
With Postgresql, the behavior is different, presumably because postgresql will automatically add a returning (which is not available in mysql).
# This will return the PK, same as mysql:
Users.insert(name='foo').execute()
# This returns ModelTupleCursorWrapper
res = Users.insert_from(
Select(columns=['a']),
['name']
).execute()
# get the pk
print(list(res)[0][0])
# let's get rid of the RETURNING clause, and the behavior is the same as mysql, which is to return 1:
res = Users.insert_from(
Select(columns=['a']),
['name']
).returning().execute()
Issue Analytics
- State:
- Created a year ago
- Comments:14 (9 by maintainers)
Top Results From Across the Web
MySQL INSERT shows more affected rows than affected
So, am I just seeing the number or rows returned from my SELECT statement, and not the number or rows affected by my...
Read more >5.4.1 mysql_affected_rows() - MySQL :: Developer Zone
It returns the number of rows changed, deleted, or inserted by the last statement if it was an UPDATE , DELETE , or...
Read more >Cursor — aiomysql 0.1.2.dev50+gbb8697d documentation
Returns the number of rows that has been produced of affected. This read-only attribute specifies the number of rows that the last Cursor.execute()...
Read more >sqlite3 — DB-API 2.0 interface for SQLite databases — Python ...
Return the next set of rows of a query result as a list . Return an empty list if no more rows are...
Read more >Working with Engines and Connections — SQLAlchemy 2.0 ...
“Insert Many Values” Behavior for INSERT statements ... as connection: result = connection.execute(text("select username from users")) for row in result: ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Sure:
The issue with the query execution I believe is fixed in 3.36 and is far more concerning than the error code. So if you check for 3.36 or newer it may be safe to use returning everywhere.
I’m going to revisit that patch and see how things are looking when I can find some free time.