I use SQLAlchemy with MySQL. In one application, I need to obtain the
server-generated primary key values of newly inserted rows by obtaining the
value of the first inserted row with the property inserted_primary_key.
However, after upgrading to version 1.4.15, this property return only
(None,).
See the following trivial example:
from sqlalchemy import create_engine
from sqlalchemy import Column, Integer, Text, MetaData, Table
engine =
create_engine('mysql+pymysql://testuser:xyz6789@localhost/testdb?charset=utf8')
metadata = MetaData()
messages = Table(
'messages', metadata,
Column('id', Integer, primary_key=True),
Column('message', Text),
)
# on first run:
# messages.create(bind=engine)
values = [
{'message': 'Hello World'},
{'message': 'Hallo Welt'},
]
with engine.begin() as conn:
result = conn.execute(messages.insert().values(values))
first_id = result.inserted_primary_key[0]
print(f'first_id = {first_id}')
Result in SQLAlchemy-1.3.15:
first_id = 1
But in SQLAlchemy-1.4.15:
first_id = None
Converting to new-style querying does not help. The new property
inserted_primary_key_rows returns just nested tuples of None whenever more
than one row is inserted. This is metioned in the documentation for
backends that do not support returned primary keys -- but MySQL obviously
does!
Any help highly appreciated. Right now I cannot think of any other solution
than sticking with 1.3.
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
http://www.sqlalchemy.org/
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable
Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sqlalchemy/dd6eb61d-da0a-48d0-88d7-ef0e3ba437d8n%40googlegroups.com.