-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow upserting by exclusion constraints and index expressions #2123
Comments
I don't have an idea about syntax, yet - but I like the idea. Even though we were noting somewhere in #2070 (long thread), that constraint names basically violate our schema isolation approach, I still think we can't work around those in general, especially not for embedding. So it seems consistent to follow up on that and allow to use them for conflict resolution, too. |
Hm, the postgresql docs mention:
https://www.postgresql.org/docs/current/sql-insert.html#SQL-ON-CONFLICT @wattroll So wouldn't using ON CONFLICT with a list columns be preferrable to ON CONSTRAINT? Are there cases which cannot be resolved in that way? |
While following https://www.postgresql.org/message-id/flat/5b238fdd-16a2-4b63-a347-c63813ebccb0%40www.fastmail.com I thought about why using constraint names in general could be a good idea. In a way a constraint name hides the implementation details of which columns are used. When used in I think that principle still applies here. The tip given in the docs is not very useful imho. If I had to change a unique index, which I had given a meaningful name (and I do that all the time, because of error reporting when inserting conflicting rows), then I would do the following:
This would not break any queries that use the constraint name - in fact it would enable them to work correctly, even when renaming columns. |
@wolfgangwalther I see the concern regarding the schema isolation. As you say, it can be seen from several perspectives. On one side sending the constraint name seems like relying on internal details of a schema. On the other hand, specifying columns to be unique-by is not much different (in terms of schema isolation), it makes an expectation that those columns would be there with those exact names and there will be a unique constraint on them of some kind. So explicitly naming a constraint can also be useful in that regard. Is there a way for us to see if the constraint has a default auto-generated name or the database administrator did provide something better? Then we could maybe get the best of both worlds by not allowing to specify a constraint name UNLESS it has been named explicitly. @steve-chavez Indeed, if it's possible to achieve what's needed by only specifying column names it's a better way as it would allow postgresql to figure out what is the best index for that query. I'll make an example out of my use-case, so that we have something to work with (and include in the docs if we go forward with this). |
It is quite different in general, because the column names can be from the exposed view schema. Schema isolation is about hiding details from the hidden table schema...
... although I don't really know which column names are used for the |
There's also a use case for supporting index expressions in The on_conflict parameter doesn't support expressions. Not sure yet how we would do it. |
Current query syntax allows
on_conflict
with a set of fields that must have a matchingUNIQUE
constraint to upsert based on them.ON CONFLICT
can also be used with explicitON CONSTRAINT <constraint-name>
directive. This is very handy as it allows upserting according to exclusion constraints. This allows upsert based on almost arbitrary business rules when the schema allows it (by defining a constraint).Current situation is a bit limiting. It is possible to define a resource which rejects certain inserts, but not possible to handle those rejections gracefully.
Will there be some pitfalls if support for this was added?
What would be a good syntax for this feature?
The text was updated successfully, but these errors were encountered: