Peewee: Support for conditional insert/update based on EXISTS/NOT EXISTS

Created on 12 Feb 2016  Â·  4Comments  Â·  Source: coleifer/peewee

I couldn't figure out how to do an "INSERT IF NOT EXISTS" according to http://stackoverflow.com/questions/15710162/conditional-insert-into-statement-in-postgres in PostgreSQL. I am now using create_or_get instead, but it would be nice if peewee would support the general "exists/not exists" in the when function.
Likewise for the UPSERT function introduced in PostgreSQL-9.5

Most helpful comment

Hi, any word on upsert support for Postgres 9.5?

All 4 comments

Here's an example of how to insert with a select query:

http://docs.peewee-orm.com/en/latest/peewee/api.html#Model.insert_from

For postgres, you can use the on_conflict method:

http://docs.peewee-orm.com/en/latest/peewee/api.html#InsertQuery.on_conflict

I'll need to check if it works with postgres, as its only been tested on
sqlite.
On Feb 12, 2016 5:39 AM, "Tiziano Müller" [email protected] wrote:

I couldn't figure out how to do an "INSERT IF NOT EXISTS" according to
http://stackoverflow.com/questions/15710162/conditional-insert-into-statement-in-postgres
in PostgreSQL. I am now using create_or_get instead, but it would be nice
if peewee would support the general "exists/not exists" in the when
function.
Likewise for the UPSERT function introduced in PostgreSQL-9.5

—
Reply to this email directly or view it on GitHub
https://github.com/coleifer/peewee/issues/853.

Ok, I tried the following:

models.TaskStatus.insert(name='done').on_conflict('IGNORE').execute()

which unfortunately generates a INSERT OR IGNORE ... which is not supported by PostgreSQL (I get a syntax error) instead of sql INSERT ... ON CONFLICT which is supposed to work with with PostgreSQL and MySQL.

Hi, any word on upsert support for Postgres 9.5?

I'm planning support for Postgres 9.5's new upsert functionality. Discussion has been consolidated into this ticket: #1307 . Please feel free to comment on that ticket with any thoughts or suggestions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GMaxera picture GMaxera  Â·  4Comments

rayzorben picture rayzorben  Â·  4Comments

megachweng picture megachweng  Â·  3Comments

alexpantyukhin picture alexpantyukhin  Â·  5Comments

serkandaglioglu picture serkandaglioglu  Â·  4Comments