Postgresql ON CONFLICT
See original GitHub issueHi,
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 if
in generate_insert
.
Thanks
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:12 (7 by maintainers)
Top GitHub Comments
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.
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.