Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: AttributeError: 'Engine' object has no attribute 'cursor' #58949

Closed
3 tasks done
brunolnetto opened this issue Jun 6, 2024 · 5 comments
Closed
3 tasks done

BUG: AttributeError: 'Engine' object has no attribute 'cursor' #58949

brunolnetto opened this issue Jun 6, 2024 · 5 comments
Labels
Bug Needs Info Clarification about behavior needed to assess issue

Comments

@brunolnetto
Copy link

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
from os import getcwd, path, getenv
from dotenv import load_dotenv
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base


# Get environment variables
host='localhost'
port=5432
user='postgres'
passw='postgres'
database_name='mydb'

# Connect to the database
db_uri = f'postgresql://{user}:{passw}@{host}:{port}/{database_name}'

# Create the database engine and session maker
engine = create_engine(db_uri)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

df=pd.DataFrame({'a': [1,2], 'b': [3,4]})

columns=[
    'a', 
    'b'
]

# Break the dataframe into chunks
df.to_sql(
    name='teste',
    if_exists='append',
    con=engine,
    index=False
)

Issue Description

I use pandas method to_sql often. On sqlalchemy update 2.2.0 and beyond, I got following error AttributeError: 'Engine' object has no attribute 'cursor'. It seems, the interface has changed compared to previous versions.

Expected Behavior

I expect to load data on table 'mydb' or append in case it exists already and have compatible columns.

Installed Versions

INSTALLED VERSIONS

commit : d9cdd2e
python : 3.12.3.final.0
python-bits : 64
OS : Windows
OS-release : 11
Version : 10.0.22621
machine : AMD64
processor : Intel64 Family 6 Model 154 Stepping 4, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : pt_BR.cp1252

pandas : 2.2.2
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0.post0
setuptools : 69.2.0
pip : 24.0
Cython : None
pytest : 8.1.1
hypothesis : None
...
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

@brunolnetto brunolnetto added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 6, 2024
@asishm
Copy link
Contributor

asishm commented Jun 6, 2024

Thanks for the report. Can you please post the full output of pd.show_versions() as required in the template (or at least your sqlalchemy version)?

@mroeschke mroeschke added Needs Info Clarification about behavior needed to assess issue and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 6, 2024
@brunolnetto
Copy link
Author

I got error AttributeError: module 'sqlalchemy' has no attribute 'show_versions' for sqlalchemy.

@asishm
Copy link
Contributor

asishm commented Jun 6, 2024

either the output of pd.show_versions() or sqlalchemy.__version__.

I'm guessing that it (sqlalchemy version) might be lower than 2.0. pandas 2.2 increased the minimum sqlalchemy version supported to 2.0

@brunolnetto
Copy link
Author

You are correct. I will close and mark as resolved for future reference

@asishm
Copy link
Contributor

asishm commented Jun 8, 2024

fwiw - there's an open issue discussing if the minimum supported sqlalchemy version can be reversed back to the 1.4.x series - #57049

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Info Clarification about behavior needed to assess issue
Projects
None yet
Development

No branches or pull requests

3 participants