Skip to content
Advertisement

Find the row violating the constraint to be added

I’m trying to add a check constraint to a table like

ALTER TABLE foo ADD CONSTRAINT bar CHECK (...);

and I get the error:

ERROR:  check constraint "bar" is violated by some row
********** Error **********

ERROR: check constraint "bar" is violated by some row
SQL state: 23514

Is there a way to have postgres (9.6) tell me at which row it fails? It would help a lot.

Advertisement

Answer

There sure is.

Select * from bar where NOT <desired check constraint>;
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement