Home > Mobile >  Postgresql check constraint gives error, but the value (text) is correct
Postgresql check constraint gives error, but the value (text) is correct

Time:10-17

I have table test created using Dbeaver with columns:

str::text

and check constraint is: (('str'::text = 'koko'::text))

that's the only column and DDL for table is:

CREATE TABLE public.test (
    str text NULL,
    CONSTRAINT test_check CHECK (('str'::text = 'koko'::text))
);

I am inserting into table:

INSERT INTO public.test(str) VALUES ('koko');

which gives error:

SQL Error [23514]: ERROR: new row for relation "test" violates check constraint "test_check"
  Detail: Failing row contains (koko).

Why is that an error if the text is correct?

CodePudding user response:

I didn't notice the comment before thinking about this, so I'll just scoop the answer. If you look at the documentation here, it is clear how the constraint should be written.

CONSTRAINT test_check CHECK (str = 'koko'::text)

  • Related