'Mark Rotteveel' via firebird-devel wrote 27.04.2024 15:03:
> And yet, I can foresee situations where there will be different handling
> required if its is known the record already exists as opposed to handling the
> case where a new record just happens to violate a unique index because *another*
> record has the same combination.
>
> Sure, there are other ways to address that, but don't act as if it is impossible
> that someone builds an application that expect a certain order of operations,
> and which breaks in unexpected ways if the order changes after a backup and
> restore.
Of course it is possible for anyone to build an application that relies on
constraint check order. People do stupid things all the way. It doesn't make
these things are right.
Let's take a wider look: one has table with two unique constraints. Any of
these constraints can be violated independently so the application has to be
prepared to handle such violations of both constraints.
a) Constraint1 is violated, application solves it in a way1.
b) Constraint2 is violated, application solves it in a way2.
c) Both constraints are violated.
c1) Violation of constraint1 is reported first, way1 is applied but in the
result constraint2 is violated anyway.
c2) Violation of constraint2 is reported first, way2 is applied but in the
result constraint1 is violated anyway.
I.e. case c) is unsolvable.
Example from the SO:
Record exists with values (1,2).
Record with values (1,1) is attempted to be inserted. PK violation happen,
insert is transformed into update, result is record (1,1) all is fine. This is
what topicstarter expects.
Record with values (2,2) is attempted to be inserted. UK violation happen. I
have no idea how they suppose to fix that but let's guess the same way: insert
is transformed into update by UK, result is record (2,2) in database.
Records (1,2) and (2,1) exist.
Record with values (1,1) is attempted to be inserted. If PK violation is
reported first, insert is transformed into update UK=1 WHERE PK=1 which violates
UK constraint. Failure.
If UK violation is reported first, insert is transformed into update PK=1 WHERE
UK=1 which violates PK constraint. Failure.
Here my fantasy is out of fuel so if you can imagine a way to resolve such
situation that depends on predictable order of constraint checks - I'm all ears.
--
WBR, SD.