Skip to content

Commit 8daa913

Browse files
committed
57539: Fixed creation unnamed table when using public schema
Problem: - Table on public schema being lost when tried to be created Solution: - Used db_schema_name argument to specify schema name of adbc_ingest
1 parent e51039a commit 8daa913

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

doc/source/whatsnew/v2.2.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Bug fixes
2424
~~~~~~~~~
2525
- :meth:`DataFrame.__dataframe__` was showing bytemask instead of bitmask for ``'string[pyarrow]'`` validity buffer (:issue:`57762`)
2626
- :meth:`DataFrame.__dataframe__` was showing non-null validity buffer (instead of ``None``) ``'string[pyarrow]'`` without missing values (:issue:`57761`)
27+
- :meth:`DataFrame.to_sql` was failing to find the right table on PostgreSQL when using the ``public`` schema (:issue:`57539`)
2728

2829
.. ---------------------------------------------------------------------------
2930
.. _whatsnew_222.other:

pandas/io/sql.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -2380,7 +2380,9 @@ def to_sql(
23802380
raise ValueError("datatypes not supported") from exc
23812381

23822382
with self.con.cursor() as cur:
2383-
total_inserted = cur.adbc_ingest(table_name, tbl, mode=mode)
2383+
total_inserted = cur.adbc_ingest(
2384+
table_name=name, data=tbl, mode=mode, db_schema_name=schema
2385+
)
23842386

23852387
self.con.commit()
23862388
return total_inserted

pandas/tests/io/test_sql.py

+48
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,54 @@ def insert_on_conflict(table, conn, keys, data_iter):
13731373
pandasSQL.drop_table("test_insert_conflict")
13741374

13751375

1376+
@pytest.mark.parametrize("conn", all_connectable)
1377+
def test_to_sql_on_public_schema(conn, request):
1378+
if "sqlite" in conn:
1379+
request.node.add_marker(
1380+
pytest.mark.xfail(
1381+
reason="sqlite database does not support schemas",
1382+
)
1383+
)
1384+
1385+
conn = request.getfixturevalue(conn)
1386+
1387+
from sqlalchemy.engine import Engine
1388+
from sqlalchemy.sql import text
1389+
1390+
create_sql = text(
1391+
"""
1392+
CREATE TABLE public.test_public_schema (
1393+
a integer PRIMARY KEY,
1394+
b numeric,
1395+
c text
1396+
);
1397+
"""
1398+
)
1399+
if isinstance(conn, Engine):
1400+
with conn.connect() as con:
1401+
with con.begin():
1402+
con.execute(create_sql)
1403+
else:
1404+
with conn.begin():
1405+
conn.execute(create_sql)
1406+
1407+
test_data = DataFrame([[1, 2.1, "a"], [2, 3.1, "b"]], columns=list("abc"))
1408+
test_data.to_sql(
1409+
name="test_public_schema",
1410+
con=conn,
1411+
if_exists="append",
1412+
index=False,
1413+
schema="public",
1414+
)
1415+
1416+
df_out = sql.read_sql_table("test_public_schema", conn, schema="public")
1417+
assert test_data.equals(df_out)
1418+
1419+
# Cleanup
1420+
with sql.SQLDatabase(conn, need_transaction=True) as pandasSQL:
1421+
pandasSQL.drop_table("test_public_schema")
1422+
1423+
13761424
@pytest.mark.parametrize("conn", mysql_connectable)
13771425
def test_insertion_method_on_conflict_update(conn, request):
13781426
# GH 14553: Example in to_sql docstring

0 commit comments

Comments
 (0)