When I insert multiple rows with sqlalchemy, I am not getting the inserted id values.
For example, when I execute the plain SQL in MS SQL Server, it display 2 rows in the resultset (the recs I inserted...1,2).
INSERT INTO dbo.emp (id, fname, lname, dob)
OUTPUT inserted.id
VALUES (1, 'Test1','Rec1','01/01/1990'), (2, 'Test2','Rec2','01/01/1990')
When I execute the same query in sqlalchemy using the below code, print statement just shows the value 2 (not 1). It looks like resultset has last row inserted. Is there any issue with below code ?
try:
sql = """\
INSERT INTO dbo.emp (id, fname, lname, dob)
OUTPUT inserted.id
VALUES (:id, :fname, :lname, :dob)
"""
stmt = text(sql)
data = [
{'id': 1, 'fname':'Test1', 'lname':'rec1', 'dob':'05/05/2000'}
,{'id': 2, 'fname': 'Test2', 'lname': 'rec2', 'dob': '05/05/2001'}
]
result = conn.execute(stmt, data)
for id in result:
print(id)
conn.commit()
except exc.IntegrityError as e:
print('Code3 - Integrity error raised', e)
conn.rollback()
except exc.SQLAlchemyError as e:
print('Code3 - Something else went wrong', e)
conn.rollback()