Skip to content
Advertisement

Postgresql constraint violated at restore

I have a problem with a constraint that is being violated by some rows when restoring my DB, but has never been violated at usage.

I have 3 tables :

some_table here is another table, which I think is not relevant for this problem.

Now what I want is to have a unicity constraint between as.id and bs.some_id through the relation table as_bs. E.g:

(Thanks @wildplasser for formatting)

This brings me to the following constraint:

Then my program has lived its life, but when I want to restore a recent backup I got an error because of this constraint.

In my backup, I removed the constraint function and the check and I can restore my backup with no problem. Of course then if I’m trying to reapply the check, I got:

So I went to the idea of finding what rows are faulty.

To do that, I joined as and bs to as_bs and grouped by the unicity group (a_id, b_id, some_id):

and I got the surprise that it did not return any row…

Now I’m just wondering what’s the problem between

  • My constraint is wrong for what I want to do
  • My unicity check just above does not check what I want to check

and anyway, I’m also wondering how a constraint that could be registered once can’t be restored now because it was violated.

Advertisement

Answer


In your current schema, (as_bs.a_id, as_bs.b_id) is unique Adding dependant columns from as or bs won’t make it more unique.


UPDATE:



Output:


User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement