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. On Tuesday, August 24, 2021 at 9:45:12 PM UTC+2 Mike Bayer wrote: > not exactly what you asked for but this seems pretty easy to use: > > > https://docs.sqlalchemy.org/en/14/orm/persistence_techniques.html#using-postgresql-on-conflict-with-returning-to-return-upserted-orm-objects > > > > On Tue, Aug 24, 2021, at 2:53 PM, Mike Bayer wrote: > > let me try to make an example > > On Tue, Aug 24, 2021, at 1:36 PM, Mike Bayer wrote: > > 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. > > > > > > > > > On Tue, Aug 24, 2021, at 12:17 PM, Anthony Catel wrote: > > 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! > > > -- > SQLAlchemy - > The Python SQL Toolkit and Object Relational Mapper > > http://www.sqlalchemy.org/ > > 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. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sqlalchemy/48c6462a-c4fd-46c7-aead-464b29a03e27n%40googlegroups.com > > <https://groups.google.com/d/msgid/sqlalchemy/48c6462a-c4fd-46c7-aead-464b29a03e27n%40googlegroups.com?utm_medium=email&utm_source=footer> > . > > > > -- > SQLAlchemy - > The Python SQL Toolkit and Object Relational Mapper > > http://www.sqlalchemy.org/ > > 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. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sqlalchemy/1dda86f7-1a70-4ba8-8b0d-fb45c3d62bc5%40www.fastmail.com > > <https://groups.google.com/d/msgid/sqlalchemy/1dda86f7-1a70-4ba8-8b0d-fb45c3d62bc5%40www.fastmail.com?utm_medium=email&utm_source=footer> > . > > > -- > SQLAlchemy - > The Python SQL Toolkit and Object Relational Mapper > > http://www.sqlalchemy.org/ > > 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. > > To view this discussion on the web visit > https://groups.google.com/d/msgid/sqlalchemy/ba6e9820-cb52-4a2a-b405-3b85833327ca%40www.fastmail.com > > <https://groups.google.com/d/msgid/sqlalchemy/ba6e9820-cb52-4a2a-b405-3b85833327ca%40www.fastmail.com?utm_medium=email&utm_source=footer> > . > > > -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/a4958852-3675-49cb-af24-8a7a322f3003n%40googlegroups.com.