When a row fails a not-null constraint, show row's contents in errdetail.

Simple extension of previous patch for CHECK constraints.
This commit is contained in:
Tom Lane 2011-11-29 18:29:18 -05:00
parent 8b08deb0d1
commit f225e4bc54
8 changed files with 23 additions and 1 deletions

View File

@ -1576,7 +1576,9 @@ ExecConstraints(ResultRelInfo *resultRelInfo,
ereport(ERROR,
(errcode(ERRCODE_NOT_NULL_VIOLATION),
errmsg("null value in column \"%s\" violates not-null constraint",
NameStr(rel->rd_att->attrs[attrChk - 1]->attname))));
NameStr(rel->rd_att->attrs[attrChk - 1]->attname)),
errdetail("Failing row contains %s.",
ExecBuildSlotValueDescription(slot, 64))));
}
}

View File

@ -599,6 +599,7 @@ insert into atacc1 (test) values (4);
-- inserting NULL should fail
insert into atacc1 (test) values(NULL);
ERROR: null value in column "test" violates not-null constraint
DETAIL: Failing row contains (null).
-- try adding a second primary key (should fail)
alter table atacc1 add constraint atacc_oid1 primary key(oid);
ERROR: multiple primary keys for table "atacc1" are not allowed
@ -664,10 +665,13 @@ ERROR: duplicate key value violates unique constraint "atacc_test1"
DETAIL: Key (test, test2)=(4, 4) already exists.
insert into atacc1 (test,test2) values (NULL,3);
ERROR: null value in column "test" violates not-null constraint
DETAIL: Failing row contains (null, 3).
insert into atacc1 (test,test2) values (3, NULL);
ERROR: null value in column "test2" violates not-null constraint
DETAIL: Failing row contains (3, null).
insert into atacc1 (test,test2) values (NULL,NULL);
ERROR: null value in column "test" violates not-null constraint
DETAIL: Failing row contains (null, null).
-- should all succeed
insert into atacc1 (test,test2) values (4,5);
insert into atacc1 (test,test2) values (5,4);
@ -683,6 +687,7 @@ ERROR: duplicate key value violates unique constraint "atacc1_pkey"
DETAIL: Key (test)=(3) already exists.
insert into atacc1 (test2, test) values (1, NULL);
ERROR: null value in column "test" violates not-null constraint
DETAIL: Failing row contains (null, 1).
drop table atacc1;
-- alter table / alter column [set/drop] not null tests
-- try altering system catalogs, should fail
@ -733,8 +738,10 @@ create table child (b varchar(255)) inherits (parent);
alter table parent alter a set not null;
insert into parent values (NULL);
ERROR: null value in column "a" violates not-null constraint
DETAIL: Failing row contains (null).
insert into child (a, b) values (NULL, 'foo');
ERROR: null value in column "a" violates not-null constraint
DETAIL: Failing row contains (null, foo).
alter table parent alter a drop not null;
insert into parent values (NULL);
insert into child (a, b) values (NULL, 'foo');
@ -746,13 +753,16 @@ delete from parent;
alter table only parent alter a set not null;
insert into parent values (NULL);
ERROR: null value in column "a" violates not-null constraint
DETAIL: Failing row contains (null).
alter table child alter a set not null;
insert into child (a, b) values (NULL, 'foo');
ERROR: null value in column "a" violates not-null constraint
DETAIL: Failing row contains (null, foo).
delete from child;
alter table child alter a set not null;
insert into child (a, b) values (NULL, 'foo');
ERROR: null value in column "a" violates not-null constraint
DETAIL: Failing row contains (null, foo).
drop table child;
drop table parent;
-- test setting and removing default values

View File

@ -206,10 +206,12 @@ INSERT INTO nulltest values ('a', NULL, 'c', 'd', 'c');
ERROR: domain dnotnull does not allow null values
INSERT INTO nulltest values ('a', 'b', NULL, 'd', 'c');
ERROR: null value in column "col3" violates not-null constraint
DETAIL: Failing row contains (a, b, null, d, c).
INSERT INTO nulltest values ('a', 'b', 'c', NULL, 'd'); -- Good
-- Test copy
COPY nulltest FROM stdin; --fail
ERROR: null value in column "col3" violates not-null constraint
DETAIL: Failing row contains (a, b, null, d, d).
CONTEXT: COPY nulltest, line 1: "a b \N d d"
COPY nulltest FROM stdin; --fail
ERROR: domain dcheck does not allow null values
@ -264,12 +266,14 @@ create table defaulttest
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "defaulttest_pkey" for table "defaulttest"
insert into defaulttest(col4) values(0); -- fails, col5 defaults to null
ERROR: null value in column "col5" violates not-null constraint
DETAIL: Failing row contains (3, 12, 5, 0, null, 88, 8000, 12.12).
alter table defaulttest alter column col5 drop default;
insert into defaulttest default values; -- succeeds, inserts domain default
-- We used to treat SET DEFAULT NULL as equivalent to DROP DEFAULT; wrong
alter table defaulttest alter column col5 set default null;
insert into defaulttest(col4) values(0); -- fails
ERROR: null value in column "col5" violates not-null constraint
DETAIL: Failing row contains (3, 12, 5, 0, null, 88, 8000, 12.12).
alter table defaulttest alter column col5 drop default;
insert into defaulttest default values;
insert into defaulttest default values;

View File

@ -539,6 +539,7 @@ CREATE TEMP TABLE z (b TEXT, PRIMARY KEY(aa, b)) inherits (a);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "z_pkey" for table "z"
INSERT INTO z VALUES (NULL, 'text'); -- should fail
ERROR: null value in column "aa" violates not-null constraint
DETAIL: Failing row contains (null, text).
-- Check UPDATE with inherited target and an inherited source table
create temp table foo(f1 int, f2 int);
create temp table foo2(f3 int) inherits (foo);

View File

@ -4,6 +4,7 @@
create table inserttest (col1 int4, col2 int4 NOT NULL, col3 text default 'testing');
insert into inserttest (col1, col2, col3) values (DEFAULT, DEFAULT, DEFAULT);
ERROR: null value in column "col2" violates not-null constraint
DETAIL: Failing row contains (null, null, testing).
insert into inserttest (col2, col3) values (3, DEFAULT);
insert into inserttest (col1, col2, col3) values (DEFAULT, 5, DEFAULT);
insert into inserttest values (DEFAULT, 5, 'test');

View File

@ -8,6 +8,7 @@ INSERT INTO serialTest VALUES ('bar');
INSERT INTO serialTest VALUES ('force', 100);
INSERT INTO serialTest VALUES ('wrong', NULL);
ERROR: null value in column "f2" violates not-null constraint
DETAIL: Failing row contains (wrong, null).
SELECT * FROM serialTest;
f1 | f2
-------+-----

View File

@ -8,6 +8,7 @@ INSERT INTO serialTest VALUES ('bar');
INSERT INTO serialTest VALUES ('force', 100);
INSERT INTO serialTest VALUES ('wrong', NULL);
ERROR: null value in column "f2" violates not-null constraint
DETAIL: Failing row contains (wrong, null).
SELECT * FROM serialTest;
f1 | f2
-------+-----

View File

@ -320,6 +320,7 @@ INSERT INTO PRIMARY_TBL VALUES (4, 'three');
INSERT INTO PRIMARY_TBL VALUES (5, 'one');
INSERT INTO PRIMARY_TBL (t) VALUES ('six');
ERROR: null value in column "i" violates not-null constraint
DETAIL: Failing row contains (null, six).
SELECT '' AS four, * FROM PRIMARY_TBL;
four | i | t
------+---+-------
@ -340,6 +341,7 @@ INSERT INTO PRIMARY_TBL VALUES (4, 'three');
INSERT INTO PRIMARY_TBL VALUES (5, 'one');
INSERT INTO PRIMARY_TBL (t) VALUES ('six');
ERROR: null value in column "i" violates not-null constraint
DETAIL: Failing row contains (null, six).
SELECT '' AS three, * FROM PRIMARY_TBL;
three | i | t
-------+---+-------