How to use pandas to do upsert in SqlAlchemy

前端 未结 1 438
鱼传尺愫
鱼传尺愫 2021-01-23 07:26

I created a table in postgresql by SqlAlchemy:

my_table = Table(\'test_table\', meta,
                         Column(\'id\', Integer,primary_key=Tr         


        
相关标签:
1条回答
  • 2021-01-23 08:23

    The set_ parameter expects a mapping with column names as keys and expressions or literals as values, but you're passing a mapping with nested dictionaries as values, i.e. df.to_dict(orient='dict'). The error "can't adapt type 'dict'" is the result of SQLAlchemy passing those dictionaries to Psycopg2 as "literals".

    Because you are trying to insert multiple rows in a single INSERT using the VALUES clause, you should use excluded in the SET actions. EXCLUDED is a special table representing the rows meant to be inserted.

    insert_statement = postgresql.insert(my_table).values(df.to_dict(orient='records'))
    upsert_statement = insert_statement.on_conflict_do_update(
        index_elements=['id'],
        set_={c.key: c for c in insert_statement.excluded if c.key != 'id'})
    conn.execute(upsert_statement)
    
    0 讨论(0)
提交回复
热议问题