pythonsqlpandaspsqlpandas-to-sql

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


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?


Solution

  • 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'