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

Postgres upsert table creation #2818

Closed
AntonMantulo opened this issue May 13, 2024 · 0 comments · Fixed by #2819
Closed

Postgres upsert table creation #2818

AntonMantulo opened this issue May 13, 2024 · 0 comments · Fixed by #2819
Labels
bug Something isn't working

Comments

@AntonMantulo
Copy link
Contributor

AntonMantulo commented May 13, 2024

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 AntonMantulo added the bug Something isn't working label May 13, 2024
@kukushking kukushking linked a pull request May 14, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant