Skip to content

BUG: to_sql(method=psql_insert_copy) not working with Connection #48136

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

Closed
2 of 3 tasks
jackieleng opened this issue Aug 18, 2022 · 2 comments
Closed
2 of 3 tasks

BUG: to_sql(method=psql_insert_copy) not working with Connection #48136

jackieleng opened this issue Aug 18, 2022 · 2 comments
Labels
Bug IO SQL to_sql, read_sql, read_sql_query Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@jackieleng
Copy link
Contributor

jackieleng commented Aug 18, 2022

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 sqlalchemy import create_engine
some_df = pd.DataFrame(...)
engine = create_engine("postgresql://postgres:password@localhost:5432/mydb")
psql_insert_copy = ... # see: https://pandas.pydata.org/docs/user_guide/io.html#io-sql-method

# This works:
some_df.to_sql("table", engine, if_exists="append", method=psql_insert_copy, index=False)

# This doesn't work:
with engine.connect() as conn:
    some_df.to_sql("table", conn, if_exists="append", method=psql_insert_copy, index=False)

Issue Description

I'm using the insertion method psql_insert_copy as described here: https://pandas.pydata.org/docs/user_guide/io.html#io-sql-method

It only works correctly when I pass an SQLAlchemy Engine object to to_sql(), i.e., data gets inserted into the table. But if I pass an SQLAlchemy Connection to to_sql() then nothing gets written into the database table.

Expected Behavior

I expect if I pass either SQLAlchemy Engine or Connection to to_sql(method=psql_insert_copy) it should both work

Installed Versions

INSTALLED VERSIONS

commit : e8093ba
python : 3.8.10.final.0
python-bits : 64
OS : Linux
OS-release : 5.10.102.1-microsoft-standard-WSL2
Version : #1 SMP Wed Mar 2 00:30:59 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.4.3
numpy : 1.22.3
pytz : 2022.1
dateutil : 2.8.2
setuptools : 44.0.0
pip : 20.0.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.9.3
jinja2 : 3.1.2
IPython : 8.3.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
markupsafe : 2.1.1
matplotlib : 3.5.2
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.1
snappy : None
sqlalchemy : 1.4.36
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

@jackieleng jackieleng added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 18, 2022
@jbrockmendel jbrockmendel added the IO SQL to_sql, read_sql, read_sql_query label Nov 25, 2022
@vroomzel
Copy link

I have the same issue. It's actually diagnosed here #46596

@mroeschke
Copy link
Member

Closing as a duplicate of #46596

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO SQL to_sql, read_sql, read_sql_query Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

4 participants