aws-sdk-pandas icon indicating copy to clipboard operation
aws-sdk-pandas copied to clipboard

Postgres upsert table creation

Open AntonMantulo opened this issue 1 year ago • 0 comments

Describe the bug

When using upsert mode in the to_sql method for Postgres, table creation is rolled back with the following exception: "No unique or exclusion constraint matching the ON CONFLICT".

Here is a link to the Postgres docs, where it is implied that when using "ON CONFLICT" clause, there has to be a unique constraint on the fields the upsert is happening: docs

How to Reproduce

import pandas as pd
import pg8000
import awswrangler as wr 

try:
    conn = pg8000.connect(
            host="localhost",
            database="db",
            user="user",
            password="password",
        )
    df = pd.DataFrame({
            "id": [1, 2, 3],
            "value": ["foo", "boo", "bar"]
        })

    wr.postgresql.to_sql(
            df=df,
            con=conn,
            table="create_test_table",
            mode="upsert",
            schema="public",
            upsert_conflict_columns=["id"],
        )
except Exception as e:
    raise e
finally:
    conn.close()

Expected behavior

A table is created with the stated on conflict columns and subsequently data is successfully upserted.

Your project

No response

Screenshots

No response

OS

Mac

Python version

3.12.3

AWS SDK for pandas version

3.7.3

Additional context

I have created a pull request with a fix.

Thanks for you work by the way, we use your sdk a lot in my company.

AntonMantulo avatar May 13 '24 17:05 AntonMantulo