Combination of (pg) on_conflict_do_update and ORM's bulk_insert_mappings

38 views
Skip to first unread message

Anthony Catel

unread,
Aug 24, 2021, 12:17:44 PMAug 24
to sqlalchemy
Hey,

I'm looking for a way to builk upsert using pg's "on_conflict_do_update"  in a way that would allow me to use a mapper classe (because the objects I want to insert/update are joined-inheritance).

bulk_insert_mappings() would allow me to use its "return_defaults" and to provide a Mapper classe. But I couldn't figure out a way to combine it with "on_conflict_do_update".

Any hack I could use to glue these two?

Thanks!

Mike Bayer

unread,
Aug 24, 2021, 1:37:17 PMAug 24
to noreply-spamdigest via sqlalchemy
you can use a mapped class directly with pg's insert() construct:

insert(MyClass).values(...).on_conflict(...)

are you referring to being able to pass instances of mapped objects to the values?   Just pull these out of each object's `__dict__`, it's much more efficient than bulk_insert_mappings.    You can get object instances back too using the technique shown at https://docs.sqlalchemy.org/en/14/orm/session_basics.html#selecting-orm-objects-inline-with-update-returning-or-insert-returning , basically create the SQL insert() statement you want, make sure it uses returning(), then use select(MyModel).from_statement(my_insert) to get object instances back.
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.

Mike Bayer

unread,
Aug 24, 2021, 2:53:44 PMAug 24
to noreply-spamdigest via sqlalchemy
let me try to make an example

Mike Bayer

unread,
Aug 24, 2021, 3:45:12 PMAug 24
to noreply-spamdigest via sqlalchemy

Anthony Catel

unread,
Aug 25, 2021, 5:15:28 AMAug 25
to sqlalchemy
Hey Mike,

Thanks for the quick reply!
I already tried to pass a mapped class to Insert() but it didn't work (because the mapper is joined-inheritance and so it needs to insert a row in both the Base parent table and the child table). Got an error about "Unconsumed column names", because the values contains (through inheritance) fields from both table.

I ended up doing a "mass" query check to separate bulk_insert_mappings and bulk_update_mappings :

 https://pastebin.com/raw/JTpSsMEw

Not the best performance deal compared to on_conflict() I guess but that's all I found for now.
Reply all
Reply to author
Forward
0 new messages