db2i insert issue with sqlalchemy session

347 views
Skip to first unread message

Siddhesh Naik

unread,
Aug 29, 2019, 12:10:55 AM8/29/19
to ibm_db
Hi,
I was trying to insert record into the table. For that I was using sql alchemy session. while inserting data into table, I got following error

AttributeError: 'pyodbc.Cursor' object has no attribute 'lastrowid'

My code snippet :
from sqlalchemy import *
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.pool import StaticPool


URL = "ibm_db_sa+pyodbc400://{user}:{password}@{host}:{port}/{database}"

engine = create_engine(URL, poolclass=StaticPool, echo=True)
meta = MetaData(bind=engine, schema="test")
db_session = sessionmaker(bind=engine)
model = declarative_base(metadata=meta)


class Client(model):
    __tablename__ = 'client'
    id = Column(Integer, autoincrement=True, nullable=False, primary_key=True)
    name = Column(String(128), nullable=True)

    def __init__(self, name):
        self.name = name

def add_client(client_details):
    session = db_session()
    client = Client(**client_details)
    session.add(client)
    session.flush()
    return True

def add_client1(client_name):
    session = db_session()
    session.execute("INSERT INTO edcservice.client(name) values('" + client_name + "')")
    return True


client_data = {"name": "test"}
add_client(client_data)
client_name = "test"
add_client1(client_name)



Both the functions (add_client(), add_client1() return same error as AttributeError: 'pyodbc.Cursor' object has no attribute 'lastrowid'

Please help

Abhinav Radke

unread,
Aug 30, 2019, 12:13:14 AM8/30/19
to ibm_db
I am able to reproduce the issue. Looking into it.

Abhinav Radke

unread,
Sep 3, 2019, 11:16:39 PM9/3/19
to ibm_db
Hi,
I have made the code changes in my private repository pasted the fix below. 
I will merge the fix to main branch after some time along with other fix.


By mean time  you can add this code to your code base.
Let me know if you still face the issue.

Thanks,
Abhinav


On Thursday, 29 August 2019 09:40:55 UTC+5:30, Siddhesh Naik wrote:

Siddhesh Naik

unread,
Sep 4, 2019, 3:51:59 AM9/4/19
to ibm_db
Its worked.
Thanks a lot.
Reply all
Reply to author
Forward
0 new messages