No such table error when replacing a table through SQLalchemy?

Viewed 390

I am using SQLalchemy and the pandas method pd.to_sql(if_exists="replace") to insert data in to a table in PostgreSQL.

Now, if the table doesn't exist, then everything works ok.

But, if the table already exists (I know it exists since I can see it in pgAdmin), I get the following error: sqlalchemy.exc.NoSuchTableError:

SQLalchemy is trying to drop this table to replace it with the new one, but it can't find the table for some reason?

Here is a snippet of the code:

ENGINE = create_engine(...)

with ENGINE.begin() as con:
   df.to_sql("table_name", con=con, index=False, if_exists="replace")

This throws the above error? I have tried specifying the schema but this also throws the same error.

Why isn't SQLalchemy finding the table even though it's there?

EDIT: If I remove the if_exists="replace", I then get the error: ValueError: Table tablename already exists.

0 Answers
Related