Replace table with dependencies using pandas to_sql's if_exists='replace'

1.5k Views Asked by At

Pandas pd.to_sql() function has the parameter if_exists='replace', which drops the table and inserts the given DataFrame. But the table I'm trying to replace is part of a view. Is there a way to replace the table and keep the view without having to delete and recreate it?

1

There are 1 best solutions below

0
Alan Millirud On BEST ANSWER

If update has same columns. You can truncate table,

con.execute('TRUNCATE table RESTART IDENTITY;')

and then run pd.to_sql with if_exists='append'