@@ -375,3 +375,76 @@ NOTICE: ALTER TABLE ... ADD CONSTRAINT will create implicit trigger(s) for FORE
375
375
ERROR: Unable to identify an operator '=' for types 'text' and 'int4'
376
376
You will have to retype this query using an explicit cast
377
377
-- temp tables should go away by themselves, need not drop them.
378
+ -- test check constraint adding
379
+ create table atacc1 ( test int );
380
+ -- add a check constraint
381
+ alter table atacc1 add constraint atacc_test1 check (test>3);
382
+ -- should fail
383
+ insert into atacc1 (test) values (2);
384
+ ERROR: ExecAppend: rejected due to CHECK constraint atacc_test1
385
+ -- should succeed
386
+ insert into atacc1 (test) values (4);
387
+ drop table atacc1;
388
+ -- let's do one where the check fails when added
389
+ create table atacc1 ( test int );
390
+ -- insert a soon to be failing row
391
+ insert into atacc1 (test) values (2);
392
+ -- add a check constraint (fails)
393
+ alter table atacc1 add constraint atacc_test1 check (test>3);
394
+ ERROR: AlterTableAddConstraint: rejected due to CHECK constraint atacc_test1
395
+ insert into atacc1 (test) values (4);
396
+ drop table atacc1;
397
+ -- let's do one where the check fails because the column doesn't exist
398
+ create table atacc1 ( test int );
399
+ -- add a check constraint (fails)
400
+ alter table atacc1 add constraint atacc_test1 check (test1>3);
401
+ ERROR: Attribute 'test1' not found
402
+ drop table atacc1;
403
+ -- something a little more complicated
404
+ create table atacc1 ( test int, test2 int, test3 int);
405
+ -- add a check constraint (fails)
406
+ alter table atacc1 add constraint atacc_test1 check (test+test2<test3*4);
407
+ -- should fail
408
+ insert into atacc1 (test,test2,test3) values (4,4,2);
409
+ ERROR: ExecAppend: rejected due to CHECK constraint atacc_test1
410
+ -- should succeed
411
+ insert into atacc1 (test,test2,test3) values (4,4,5);
412
+ drop table atacc1;
413
+ -- lets do some naming tests
414
+ create table atacc1 (test int check (test>3), test2 int);
415
+ alter table atacc1 add check (test2>test);
416
+ -- should fail for $2
417
+ insert into atacc1 (test2, test) values (3, 4);
418
+ ERROR: ExecAppend: rejected due to CHECK constraint $2
419
+ drop table atacc1;
420
+ -- inheritance related tests
421
+ create table atacc1 (test int);
422
+ create table atacc2 (test2 int);
423
+ create table atacc3 (test3 int) inherits (atacc1, atacc2);
424
+ alter table atacc2 add constraint foo check (test2>0);
425
+ -- fail and then succeed on atacc2
426
+ insert into atacc2 (test2) values (-3);
427
+ ERROR: ExecAppend: rejected due to CHECK constraint foo
428
+ insert into atacc2 (test2) values (3);
429
+ -- fail and then succeed on atacc3
430
+ insert into atacc3 (test2) values (-3);
431
+ ERROR: ExecAppend: rejected due to CHECK constraint foo
432
+ insert into atacc3 (test2) values (3);
433
+ drop table atacc3;
434
+ drop table atacc2;
435
+ drop table atacc1;
436
+ -- let's try only to add only to the parent
437
+ create table atacc1 (test int);
438
+ create table atacc2 (test2 int);
439
+ create table atacc3 (test3 int) inherits (atacc1, atacc2);
440
+ alter table only atacc2 add constraint foo check (test2>0);
441
+ -- fail and then succeed on atacc2
442
+ insert into atacc2 (test2) values (-3);
443
+ ERROR: ExecAppend: rejected due to CHECK constraint foo
444
+ insert into atacc2 (test2) values (3);
445
+ -- both succeed on atacc3
446
+ insert into atacc3 (test2) values (-3);
447
+ insert into atacc3 (test2) values (3);
448
+ drop table atacc3;
449
+ drop table atacc2;
450
+ drop table atacc1;
0 commit comments