question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Postgresql ON CONFLICT

See original GitHub issue

Hi,

we can read in documentation that INSERT … ON CONFLICT is planed to be supported soon. I’m currently using some home made query to do it, but I think it would be more stable to add support for it in peewee. I would be glad to give a try but I have some question before creating a merge request.

Looking at generate_insert (https://github.com/coleifer/peewee/blob/master/peewee.py#L1912) I’m wondering if the ON CONFLICT should be added as an upsert or as a on_conflict.

In any case the general construction of the query is rather different for sqlite ON CONFLICT is at the beginning of the query while for postgresql it is after the values. Is it ok to add some specific variable on Database class such as on_conflict_position and then do some ifin generate_insert.

Thanks

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
coleifercommented, Jul 27, 2016

One thing I’ve run into implementing this is the requirement that you must manually specify the constraint you expect to be violated.

I’m wondering how much of the INSERT ... ON CONFLICT API should be exposed to end-users? The full spec allows for a WHERE clause in the conflict target and conflict action. What a shit-show!

There was a patch submitted to PokemonGo-Map (https://github.com/AHAAAAAAA/PokemonGo-Map/commit/ae13302c75f6005aa4c33e13aac9b98c71dc0bd7) that added very basic Postgresql upsert support to Peewee. It only covers violations of the primary key constraint, which seems sensible for an application, but in practice I am sure it needs to be more flexible for general usage.

No constraint needs to be specified if the resulting action is “DO NOTHING”, but in many cases I would guess that people would instead want the “DO UPDATE SET” logic to update the existing row.

My current thinking is that, while I had hoped the approach from the PokemonGo-Map project would work, I think I need to build something that more closely matches Postgresql’s APIs.

1reaction
coleifercommented, Jul 26, 2016

The rewrite will be the 3.0 release, so as not to rock the boat for existing Peewee users.

I’d like to get upsert in 2.8 as it will allow people to use the feature without worrying about a significant upgrade.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation: 15: INSERT - PostgreSQL
INSERT with an ON CONFLICT DO UPDATE clause is a “deterministic” statement. This means that the command will not be allowed to affect...
Read more >
How to use `INSERT ON CONFLICT` to upsert data in ... - Prisma
PostgreSQL's INSERT...ON CONFLICT construct allows you to choose between two options when a proposed record conflicts with an existing record. Both DO NOTHING ......
Read more >
PostgreSQL Upsert Using INSERT ON ... - PostgreSQL Tutorial
This tutorial shows you how to use the PostgreSQL upsert feature to insert or update data if the row that is being inserted...
Read more >
Use INSERT ON CONFLICT to overwrite data - AnalyticDB for ...
Use INSERT ON CONFLICT to overwrite data,AnalyticDB for PostgreSQL:This topic describes how to use INSERT ON CONFLICT to overwrite data in ...
Read more >
PostgreSQL ON CONFLICT - eduCBA
PostgreSQL on conflict is used to insert the data in the same row twice, which the constraint or column in PostgreSQL identifies values....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found