question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Insert from_select Mysql returns number of rows affected, instead of lastrowid/pk

See original GitHub issue

Using 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:closed
  • Created a year ago
  • Comments:14 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
coleifercommented, Mar 27, 2022

Off the top of your head, do you know if I would be able to detect if a user is using MariaDB?

Sure:

conn = db.connection()  # Get db-api2 conn
print(conn.server_version) # or
print(conn.get_server_info())
# 5.5.5-10.7.3-MariaDB-1:10.7.3+maria~bullseye-log
0reactions
coleifercommented, Mar 28, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found