1

I want to create or update objects from a list of dicts using sqlalchemy.

Doing something like: session.bulk_update_mappings(MyObject, list_of_dicts)

And the problem is that I am getting integrity error, for cases when I there are some from the list_of_dicts are not in the database.

So I am kind of looking for a way to combine bulk_update_mappings and bulk_insert_mappings for one set of objects.

Oleg Tarasenko
  • 9,324
  • 18
  • 73
  • 102

1 Answers1

0

This is DB-specific. Take a look at this answer: SQLite UPSERT - ON DUPLICATE KEY UPDATE

In MySQL we solve this problem by using

INSERT ... ON DUPLICATE KEY UPDATE ... 

looks like you can achieve the same result in sqlite by using

INSERT OR IGNORE INTO ... UPDATE ...

make sure you have a proper primary (or unique) key defined so the DB can identify the dups.

Community
  • 1
  • 1
MOCKBA
  • 1,680
  • 11
  • 19