茅侃侃
这是一个关于如何使用 postgres 的ON CONFLICT DO NOTHING示例to_sql# import postgres specific insertfrom sqlalchemy.dialects.postgresql import insertdef to_sql_on_conflict_do_nothing(pd_table, conn, keys, data_iter): # This is very similar to the default to_sql function in pandas # Only the conn.execute line is changed data = [dict(zip(keys, row)) for row in data_iter] conn.execute(insert(pd_table.table).on_conflict_do_nothing(), data)conn = engine.connect()df.to_sql("some_table", conn, if_exists="append", index=False, method=to_sql_on_conflict_do_nothing)