~ubuntu-branches/ubuntu/hardy/postgresql-8.4/hardy-backports

« back to all changes in this revision

Viewing changes to src/test/regress/expected/foreign_key.out

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-03-20 12:00:13 UTC
  • Revision ID: james.westby@ubuntu.com-20090320120013-hogj7egc5mjncc5g
Tags: upstream-8.4~0cvs20090328
ImportĀ upstreamĀ versionĀ 8.4~0cvs20090328

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
--
 
2
-- FOREIGN KEY
 
3
--
 
4
-- MATCH FULL
 
5
--
 
6
-- First test, check and cascade
 
7
--
 
8
CREATE TABLE PKTABLE ( ptest1 int PRIMARY KEY, ptest2 text );
 
9
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
 
10
CREATE TABLE FKTABLE ( ftest1 int REFERENCES PKTABLE MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE, ftest2 int );
 
11
-- Insert test data into PKTABLE
 
12
INSERT INTO PKTABLE VALUES (1, 'Test1');
 
13
INSERT INTO PKTABLE VALUES (2, 'Test2');
 
14
INSERT INTO PKTABLE VALUES (3, 'Test3');
 
15
INSERT INTO PKTABLE VALUES (4, 'Test4');
 
16
INSERT INTO PKTABLE VALUES (5, 'Test5');
 
17
-- Insert successful rows into FK TABLE
 
18
INSERT INTO FKTABLE VALUES (1, 2);
 
19
INSERT INTO FKTABLE VALUES (2, 3);
 
20
INSERT INTO FKTABLE VALUES (3, 4);
 
21
INSERT INTO FKTABLE VALUES (NULL, 1);
 
22
-- Insert a failed row into FK TABLE
 
23
INSERT INTO FKTABLE VALUES (100, 2);
 
24
ERROR:  insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey"
 
25
DETAIL:  Key (ftest1)=(100) is not present in table "pktable".
 
26
-- Check FKTABLE
 
27
SELECT * FROM FKTABLE;
 
28
 ftest1 | ftest2 
 
29
--------+--------
 
30
      1 |      2
 
31
      2 |      3
 
32
      3 |      4
 
33
        |      1
 
34
(4 rows)
 
35
 
 
36
-- Delete a row from PK TABLE
 
37
DELETE FROM PKTABLE WHERE ptest1=1;
 
38
-- Check FKTABLE for removal of matched row
 
39
SELECT * FROM FKTABLE;
 
40
 ftest1 | ftest2 
 
41
--------+--------
 
42
      2 |      3
 
43
      3 |      4
 
44
        |      1
 
45
(3 rows)
 
46
 
 
47
-- Update a row from PK TABLE
 
48
UPDATE PKTABLE SET ptest1=1 WHERE ptest1=2;
 
49
-- Check FKTABLE for update of matched row
 
50
SELECT * FROM FKTABLE;
 
51
 ftest1 | ftest2 
 
52
--------+--------
 
53
      3 |      4
 
54
        |      1
 
55
      1 |      3
 
56
(3 rows)
 
57
 
 
58
DROP TABLE FKTABLE;
 
59
DROP TABLE PKTABLE;
 
60
--
 
61
-- check set NULL and table constraint on multiple columns
 
62
--
 
63
CREATE TABLE PKTABLE ( ptest1 int, ptest2 int, ptest3 text, PRIMARY KEY(ptest1, ptest2) );
 
64
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
 
65
CREATE TABLE FKTABLE ( ftest1 int, ftest2 int, ftest3 int, CONSTRAINT constrname FOREIGN KEY(ftest1, ftest2) 
 
66
                       REFERENCES PKTABLE MATCH FULL ON DELETE SET NULL ON UPDATE SET NULL);
 
67
-- Test comments
 
68
COMMENT ON CONSTRAINT constrname_wrong ON FKTABLE IS 'fk constraint comment';
 
69
ERROR:  constraint "constrname_wrong" for table "fktable" does not exist
 
70
COMMENT ON CONSTRAINT constrname ON FKTABLE IS 'fk constraint comment';
 
71
COMMENT ON CONSTRAINT constrname ON FKTABLE IS NULL;
 
72
-- Insert test data into PKTABLE
 
73
INSERT INTO PKTABLE VALUES (1, 2, 'Test1');
 
74
INSERT INTO PKTABLE VALUES (1, 3, 'Test1-2');
 
75
INSERT INTO PKTABLE VALUES (2, 4, 'Test2');
 
76
INSERT INTO PKTABLE VALUES (3, 6, 'Test3');
 
77
INSERT INTO PKTABLE VALUES (4, 8, 'Test4');
 
78
INSERT INTO PKTABLE VALUES (5, 10, 'Test5');
 
79
-- Insert successful rows into FK TABLE
 
80
INSERT INTO FKTABLE VALUES (1, 2, 4);
 
81
INSERT INTO FKTABLE VALUES (1, 3, 5);
 
82
INSERT INTO FKTABLE VALUES (2, 4, 8);
 
83
INSERT INTO FKTABLE VALUES (3, 6, 12);
 
84
INSERT INTO FKTABLE VALUES (NULL, NULL, 0);
 
85
-- Insert failed rows into FK TABLE
 
86
INSERT INTO FKTABLE VALUES (100, 2, 4);
 
87
ERROR:  insert or update on table "fktable" violates foreign key constraint "constrname"
 
88
DETAIL:  Key (ftest1,ftest2)=(100,2) is not present in table "pktable".
 
89
INSERT INTO FKTABLE VALUES (2, 2, 4);
 
90
ERROR:  insert or update on table "fktable" violates foreign key constraint "constrname"
 
91
DETAIL:  Key (ftest1,ftest2)=(2,2) is not present in table "pktable".
 
92
INSERT INTO FKTABLE VALUES (NULL, 2, 4);
 
93
ERROR:  insert or update on table "fktable" violates foreign key constraint "constrname"
 
94
DETAIL:  MATCH FULL does not allow mixing of null and nonnull key values.
 
95
INSERT INTO FKTABLE VALUES (1, NULL, 4);
 
96
ERROR:  insert or update on table "fktable" violates foreign key constraint "constrname"
 
97
DETAIL:  MATCH FULL does not allow mixing of null and nonnull key values.
 
98
-- Check FKTABLE
 
99
SELECT * FROM FKTABLE;
 
100
 ftest1 | ftest2 | ftest3 
 
101
--------+--------+--------
 
102
      1 |      2 |      4
 
103
      1 |      3 |      5
 
104
      2 |      4 |      8
 
105
      3 |      6 |     12
 
106
        |        |      0
 
107
(5 rows)
 
108
 
 
109
-- Delete a row from PK TABLE
 
110
DELETE FROM PKTABLE WHERE ptest1=1 and ptest2=2;
 
111
-- Check FKTABLE for removal of matched row
 
112
SELECT * FROM FKTABLE;
 
113
 ftest1 | ftest2 | ftest3 
 
114
--------+--------+--------
 
115
      1 |      3 |      5
 
116
      2 |      4 |      8
 
117
      3 |      6 |     12
 
118
        |        |      0
 
119
        |        |      4
 
120
(5 rows)
 
121
 
 
122
-- Delete another row from PK TABLE
 
123
DELETE FROM PKTABLE WHERE ptest1=5 and ptest2=10;
 
124
-- Check FKTABLE (should be no change)
 
125
SELECT * FROM FKTABLE;
 
126
 ftest1 | ftest2 | ftest3 
 
127
--------+--------+--------
 
128
      1 |      3 |      5
 
129
      2 |      4 |      8
 
130
      3 |      6 |     12
 
131
        |        |      0
 
132
        |        |      4
 
133
(5 rows)
 
134
 
 
135
-- Update a row from PK TABLE
 
136
UPDATE PKTABLE SET ptest1=1 WHERE ptest1=2;
 
137
-- Check FKTABLE for update of matched row
 
138
SELECT * FROM FKTABLE;
 
139
 ftest1 | ftest2 | ftest3 
 
140
--------+--------+--------
 
141
      1 |      3 |      5
 
142
      3 |      6 |     12
 
143
        |        |      0
 
144
        |        |      4
 
145
        |        |      8
 
146
(5 rows)
 
147
 
 
148
-- Try altering the column type where foreign keys are involved
 
149
ALTER TABLE PKTABLE ALTER COLUMN ptest1 TYPE bigint;
 
150
ALTER TABLE FKTABLE ALTER COLUMN ftest1 TYPE bigint;
 
151
SELECT * FROM PKTABLE;
 
152
 ptest1 | ptest2 | ptest3  
 
153
--------+--------+---------
 
154
      1 |      3 | Test1-2
 
155
      3 |      6 | Test3
 
156
      4 |      8 | Test4
 
157
      1 |      4 | Test2
 
158
(4 rows)
 
159
 
 
160
SELECT * FROM FKTABLE;
 
161
 ftest1 | ftest2 | ftest3 
 
162
--------+--------+--------
 
163
      1 |      3 |      5
 
164
      3 |      6 |     12
 
165
        |        |      0
 
166
        |        |      4
 
167
        |        |      8
 
168
(5 rows)
 
169
 
 
170
DROP TABLE PKTABLE CASCADE;
 
171
NOTICE:  drop cascades to constraint constrname on table fktable
 
172
DROP TABLE FKTABLE;
 
173
--
 
174
-- check set default and table constraint on multiple columns
 
175
--
 
176
CREATE TABLE PKTABLE ( ptest1 int, ptest2 int, ptest3 text, PRIMARY KEY(ptest1, ptest2) );
 
177
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
 
178
CREATE TABLE FKTABLE ( ftest1 int DEFAULT -1, ftest2 int DEFAULT -2, ftest3 int, CONSTRAINT constrname2 FOREIGN KEY(ftest1, ftest2) 
 
179
                       REFERENCES PKTABLE MATCH FULL ON DELETE SET DEFAULT ON UPDATE SET DEFAULT);
 
180
-- Insert a value in PKTABLE for default
 
181
INSERT INTO PKTABLE VALUES (-1, -2, 'The Default!');
 
182
-- Insert test data into PKTABLE
 
183
INSERT INTO PKTABLE VALUES (1, 2, 'Test1');
 
184
INSERT INTO PKTABLE VALUES (1, 3, 'Test1-2');
 
185
INSERT INTO PKTABLE VALUES (2, 4, 'Test2');
 
186
INSERT INTO PKTABLE VALUES (3, 6, 'Test3');
 
187
INSERT INTO PKTABLE VALUES (4, 8, 'Test4');
 
188
INSERT INTO PKTABLE VALUES (5, 10, 'Test5');
 
189
-- Insert successful rows into FK TABLE
 
190
INSERT INTO FKTABLE VALUES (1, 2, 4);
 
191
INSERT INTO FKTABLE VALUES (1, 3, 5);
 
192
INSERT INTO FKTABLE VALUES (2, 4, 8);
 
193
INSERT INTO FKTABLE VALUES (3, 6, 12);
 
194
INSERT INTO FKTABLE VALUES (NULL, NULL, 0);
 
195
-- Insert failed rows into FK TABLE
 
196
INSERT INTO FKTABLE VALUES (100, 2, 4);
 
197
ERROR:  insert or update on table "fktable" violates foreign key constraint "constrname2"
 
198
DETAIL:  Key (ftest1,ftest2)=(100,2) is not present in table "pktable".
 
199
INSERT INTO FKTABLE VALUES (2, 2, 4);
 
200
ERROR:  insert or update on table "fktable" violates foreign key constraint "constrname2"
 
201
DETAIL:  Key (ftest1,ftest2)=(2,2) is not present in table "pktable".
 
202
INSERT INTO FKTABLE VALUES (NULL, 2, 4);
 
203
ERROR:  insert or update on table "fktable" violates foreign key constraint "constrname2"
 
204
DETAIL:  MATCH FULL does not allow mixing of null and nonnull key values.
 
205
INSERT INTO FKTABLE VALUES (1, NULL, 4);
 
206
ERROR:  insert or update on table "fktable" violates foreign key constraint "constrname2"
 
207
DETAIL:  MATCH FULL does not allow mixing of null and nonnull key values.
 
208
-- Check FKTABLE
 
209
SELECT * FROM FKTABLE;
 
210
 ftest1 | ftest2 | ftest3 
 
211
--------+--------+--------
 
212
      1 |      2 |      4
 
213
      1 |      3 |      5
 
214
      2 |      4 |      8
 
215
      3 |      6 |     12
 
216
        |        |      0
 
217
(5 rows)
 
218
 
 
219
-- Delete a row from PK TABLE
 
220
DELETE FROM PKTABLE WHERE ptest1=1 and ptest2=2;
 
221
-- Check FKTABLE to check for removal
 
222
SELECT * FROM FKTABLE;
 
223
 ftest1 | ftest2 | ftest3 
 
224
--------+--------+--------
 
225
      1 |      3 |      5
 
226
      2 |      4 |      8
 
227
      3 |      6 |     12
 
228
        |        |      0
 
229
     -1 |     -2 |      4
 
230
(5 rows)
 
231
 
 
232
-- Delete another row from PK TABLE
 
233
DELETE FROM PKTABLE WHERE ptest1=5 and ptest2=10;
 
234
-- Check FKTABLE (should be no change)
 
235
SELECT * FROM FKTABLE;
 
236
 ftest1 | ftest2 | ftest3 
 
237
--------+--------+--------
 
238
      1 |      3 |      5
 
239
      2 |      4 |      8
 
240
      3 |      6 |     12
 
241
        |        |      0
 
242
     -1 |     -2 |      4
 
243
(5 rows)
 
244
 
 
245
-- Update a row from PK TABLE
 
246
UPDATE PKTABLE SET ptest1=1 WHERE ptest1=2;
 
247
-- Check FKTABLE for update of matched row
 
248
SELECT * FROM FKTABLE;
 
249
 ftest1 | ftest2 | ftest3 
 
250
--------+--------+--------
 
251
      1 |      3 |      5
 
252
      3 |      6 |     12
 
253
        |        |      0
 
254
     -1 |     -2 |      4
 
255
     -1 |     -2 |      8
 
256
(5 rows)
 
257
 
 
258
-- this should fail for lack of CASCADE
 
259
DROP TABLE PKTABLE;
 
260
ERROR:  cannot drop table pktable because other objects depend on it
 
261
DETAIL:  constraint constrname2 on table fktable depends on table pktable
 
262
HINT:  Use DROP ... CASCADE to drop the dependent objects too.
 
263
DROP TABLE PKTABLE CASCADE;
 
264
NOTICE:  drop cascades to constraint constrname2 on table fktable
 
265
DROP TABLE FKTABLE;
 
266
--
 
267
-- First test, check with no on delete or on update
 
268
--
 
269
CREATE TABLE PKTABLE ( ptest1 int PRIMARY KEY, ptest2 text );
 
270
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
 
271
CREATE TABLE FKTABLE ( ftest1 int REFERENCES PKTABLE MATCH FULL, ftest2 int );
 
272
-- Insert test data into PKTABLE
 
273
INSERT INTO PKTABLE VALUES (1, 'Test1');
 
274
INSERT INTO PKTABLE VALUES (2, 'Test2');
 
275
INSERT INTO PKTABLE VALUES (3, 'Test3');
 
276
INSERT INTO PKTABLE VALUES (4, 'Test4');
 
277
INSERT INTO PKTABLE VALUES (5, 'Test5');
 
278
-- Insert successful rows into FK TABLE
 
279
INSERT INTO FKTABLE VALUES (1, 2);
 
280
INSERT INTO FKTABLE VALUES (2, 3);
 
281
INSERT INTO FKTABLE VALUES (3, 4);
 
282
INSERT INTO FKTABLE VALUES (NULL, 1);
 
283
-- Insert a failed row into FK TABLE
 
284
INSERT INTO FKTABLE VALUES (100, 2);
 
285
ERROR:  insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey"
 
286
DETAIL:  Key (ftest1)=(100) is not present in table "pktable".
 
287
-- Check FKTABLE
 
288
SELECT * FROM FKTABLE;
 
289
 ftest1 | ftest2 
 
290
--------+--------
 
291
      1 |      2
 
292
      2 |      3
 
293
      3 |      4
 
294
        |      1
 
295
(4 rows)
 
296
 
 
297
-- Check PKTABLE
 
298
SELECT * FROM PKTABLE;
 
299
 ptest1 | ptest2 
 
300
--------+--------
 
301
      1 | Test1
 
302
      2 | Test2
 
303
      3 | Test3
 
304
      4 | Test4
 
305
      5 | Test5
 
306
(5 rows)
 
307
 
 
308
-- Delete a row from PK TABLE (should fail)
 
309
DELETE FROM PKTABLE WHERE ptest1=1;
 
310
ERROR:  update or delete on table "pktable" violates foreign key constraint "fktable_ftest1_fkey" on table "fktable"
 
311
DETAIL:  Key (ptest1)=(1) is still referenced from table "fktable".
 
312
-- Delete a row from PK TABLE (should succeed)
 
313
DELETE FROM PKTABLE WHERE ptest1=5;
 
314
-- Check PKTABLE for deletes
 
315
SELECT * FROM PKTABLE;
 
316
 ptest1 | ptest2 
 
317
--------+--------
 
318
      1 | Test1
 
319
      2 | Test2
 
320
      3 | Test3
 
321
      4 | Test4
 
322
(4 rows)
 
323
 
 
324
-- Update a row from PK TABLE (should fail)
 
325
UPDATE PKTABLE SET ptest1=0 WHERE ptest1=2;
 
326
ERROR:  update or delete on table "pktable" violates foreign key constraint "fktable_ftest1_fkey" on table "fktable"
 
327
DETAIL:  Key (ptest1)=(2) is still referenced from table "fktable".
 
328
-- Update a row from PK TABLE (should succeed)
 
329
UPDATE PKTABLE SET ptest1=0 WHERE ptest1=4;
 
330
-- Check PKTABLE for updates
 
331
SELECT * FROM PKTABLE;
 
332
 ptest1 | ptest2 
 
333
--------+--------
 
334
      1 | Test1
 
335
      2 | Test2
 
336
      3 | Test3
 
337
      0 | Test4
 
338
(4 rows)
 
339
 
 
340
DROP TABLE FKTABLE;
 
341
DROP TABLE PKTABLE;
 
342
-- MATCH unspecified
 
343
-- Base test restricting update/delete
 
344
CREATE TABLE PKTABLE ( ptest1 int, ptest2 int, ptest3 int, ptest4 text, PRIMARY KEY(ptest1, ptest2, ptest3) );
 
345
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
 
346
CREATE TABLE FKTABLE ( ftest1 int, ftest2 int, ftest3 int, ftest4 int,  CONSTRAINT constrname3
 
347
                        FOREIGN KEY(ftest1, ftest2, ftest3) REFERENCES PKTABLE);
 
348
-- Insert Primary Key values
 
349
INSERT INTO PKTABLE VALUES (1, 2, 3, 'test1');
 
350
INSERT INTO PKTABLE VALUES (1, 3, 3, 'test2');
 
351
INSERT INTO PKTABLE VALUES (2, 3, 4, 'test3');
 
352
INSERT INTO PKTABLE VALUES (2, 4, 5, 'test4');
 
353
-- Insert Foreign Key values
 
354
INSERT INTO FKTABLE VALUES (1, 2, 3, 1); 
 
355
INSERT INTO FKTABLE VALUES (NULL, 2, 3, 2);
 
356
INSERT INTO FKTABLE VALUES (2, NULL, 3, 3);
 
357
INSERT INTO FKTABLE VALUES (NULL, 2, 7, 4);
 
358
INSERT INTO FKTABLE VALUES (NULL, 3, 4, 5);
 
359
-- Insert a failed values
 
360
INSERT INTO FKTABLE VALUES (1, 2, 7, 6);
 
361
ERROR:  insert or update on table "fktable" violates foreign key constraint "constrname3"
 
362
DETAIL:  Key (ftest1,ftest2,ftest3)=(1,2,7) is not present in table "pktable".
 
363
-- Show FKTABLE
 
364
SELECT * from FKTABLE;
 
365
 ftest1 | ftest2 | ftest3 | ftest4 
 
366
--------+--------+--------+--------
 
367
      1 |      2 |      3 |      1
 
368
        |      2 |      3 |      2
 
369
      2 |        |      3 |      3
 
370
        |      2 |      7 |      4
 
371
        |      3 |      4 |      5
 
372
(5 rows)
 
373
 
 
374
-- Try to update something that should fail
 
375
UPDATE PKTABLE set ptest2=5 where ptest2=2;
 
376
ERROR:  update or delete on table "pktable" violates foreign key constraint "constrname3" on table "fktable"
 
377
DETAIL:  Key (ptest1,ptest2,ptest3)=(1,2,3) is still referenced from table "fktable".
 
378
-- Try to update something that should succeed
 
379
UPDATE PKTABLE set ptest1=1 WHERE ptest2=3;
 
380
-- Try to delete something that should fail
 
381
DELETE FROM PKTABLE where ptest1=1 and ptest2=2 and ptest3=3;
 
382
ERROR:  update or delete on table "pktable" violates foreign key constraint "constrname3" on table "fktable"
 
383
DETAIL:  Key (ptest1,ptest2,ptest3)=(1,2,3) is still referenced from table "fktable".
 
384
-- Try to delete something that should work
 
385
DELETE FROM PKTABLE where ptest1=2;
 
386
-- Show PKTABLE and FKTABLE
 
387
SELECT * from PKTABLE;
 
388
 ptest1 | ptest2 | ptest3 | ptest4 
 
389
--------+--------+--------+--------
 
390
      1 |      2 |      3 | test1
 
391
      1 |      3 |      3 | test2
 
392
      1 |      3 |      4 | test3
 
393
(3 rows)
 
394
 
 
395
SELECT * from FKTABLE;
 
396
 ftest1 | ftest2 | ftest3 | ftest4 
 
397
--------+--------+--------+--------
 
398
      1 |      2 |      3 |      1
 
399
        |      2 |      3 |      2
 
400
      2 |        |      3 |      3
 
401
        |      2 |      7 |      4
 
402
        |      3 |      4 |      5
 
403
(5 rows)
 
404
 
 
405
DROP TABLE FKTABLE;
 
406
DROP TABLE PKTABLE;
 
407
-- cascade update/delete
 
408
CREATE TABLE PKTABLE ( ptest1 int, ptest2 int, ptest3 int, ptest4 text, PRIMARY KEY(ptest1, ptest2, ptest3) );
 
409
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
 
410
CREATE TABLE FKTABLE ( ftest1 int, ftest2 int, ftest3 int, ftest4 int,  CONSTRAINT constrname3
 
411
                        FOREIGN KEY(ftest1, ftest2, ftest3) REFERENCES PKTABLE
 
412
                        ON DELETE CASCADE ON UPDATE CASCADE);
 
413
-- Insert Primary Key values
 
414
INSERT INTO PKTABLE VALUES (1, 2, 3, 'test1');
 
415
INSERT INTO PKTABLE VALUES (1, 3, 3, 'test2');
 
416
INSERT INTO PKTABLE VALUES (2, 3, 4, 'test3');
 
417
INSERT INTO PKTABLE VALUES (2, 4, 5, 'test4');
 
418
-- Insert Foreign Key values
 
419
INSERT INTO FKTABLE VALUES (1, 2, 3, 1); 
 
420
INSERT INTO FKTABLE VALUES (NULL, 2, 3, 2);
 
421
INSERT INTO FKTABLE VALUES (2, NULL, 3, 3);
 
422
INSERT INTO FKTABLE VALUES (NULL, 2, 7, 4);
 
423
INSERT INTO FKTABLE VALUES (NULL, 3, 4, 5);
 
424
-- Insert a failed values
 
425
INSERT INTO FKTABLE VALUES (1, 2, 7, 6);
 
426
ERROR:  insert or update on table "fktable" violates foreign key constraint "constrname3"
 
427
DETAIL:  Key (ftest1,ftest2,ftest3)=(1,2,7) is not present in table "pktable".
 
428
-- Show FKTABLE
 
429
SELECT * from FKTABLE;
 
430
 ftest1 | ftest2 | ftest3 | ftest4 
 
431
--------+--------+--------+--------
 
432
      1 |      2 |      3 |      1
 
433
        |      2 |      3 |      2
 
434
      2 |        |      3 |      3
 
435
        |      2 |      7 |      4
 
436
        |      3 |      4 |      5
 
437
(5 rows)
 
438
 
 
439
-- Try to update something that will cascade
 
440
UPDATE PKTABLE set ptest2=5 where ptest2=2;
 
441
-- Try to update something that should not cascade
 
442
UPDATE PKTABLE set ptest1=1 WHERE ptest2=3;
 
443
-- Show PKTABLE and FKTABLE
 
444
SELECT * from PKTABLE;
 
445
 ptest1 | ptest2 | ptest3 | ptest4 
 
446
--------+--------+--------+--------
 
447
      2 |      4 |      5 | test4
 
448
      1 |      5 |      3 | test1
 
449
      1 |      3 |      3 | test2
 
450
      1 |      3 |      4 | test3
 
451
(4 rows)
 
452
 
 
453
SELECT * from FKTABLE;
 
454
 ftest1 | ftest2 | ftest3 | ftest4 
 
455
--------+--------+--------+--------
 
456
        |      2 |      3 |      2
 
457
      2 |        |      3 |      3
 
458
        |      2 |      7 |      4
 
459
        |      3 |      4 |      5
 
460
      1 |      5 |      3 |      1
 
461
(5 rows)
 
462
 
 
463
-- Try to delete something that should cascade
 
464
DELETE FROM PKTABLE where ptest1=1 and ptest2=5 and ptest3=3;
 
465
-- Show PKTABLE and FKTABLE
 
466
SELECT * from PKTABLE;
 
467
 ptest1 | ptest2 | ptest3 | ptest4 
 
468
--------+--------+--------+--------
 
469
      2 |      4 |      5 | test4
 
470
      1 |      3 |      3 | test2
 
471
      1 |      3 |      4 | test3
 
472
(3 rows)
 
473
 
 
474
SELECT * from FKTABLE;
 
475
 ftest1 | ftest2 | ftest3 | ftest4 
 
476
--------+--------+--------+--------
 
477
        |      2 |      3 |      2
 
478
      2 |        |      3 |      3
 
479
        |      2 |      7 |      4
 
480
        |      3 |      4 |      5
 
481
(4 rows)
 
482
 
 
483
-- Try to delete something that should not have a cascade
 
484
DELETE FROM PKTABLE where ptest1=2;
 
485
-- Show PKTABLE and FKTABLE
 
486
SELECT * from PKTABLE;
 
487
 ptest1 | ptest2 | ptest3 | ptest4 
 
488
--------+--------+--------+--------
 
489
      1 |      3 |      3 | test2
 
490
      1 |      3 |      4 | test3
 
491
(2 rows)
 
492
 
 
493
SELECT * from FKTABLE;
 
494
 ftest1 | ftest2 | ftest3 | ftest4 
 
495
--------+--------+--------+--------
 
496
        |      2 |      3 |      2
 
497
      2 |        |      3 |      3
 
498
        |      2 |      7 |      4
 
499
        |      3 |      4 |      5
 
500
(4 rows)
 
501
 
 
502
DROP TABLE FKTABLE;
 
503
DROP TABLE PKTABLE;
 
504
-- set null update / set default delete
 
505
CREATE TABLE PKTABLE ( ptest1 int, ptest2 int, ptest3 int, ptest4 text, PRIMARY KEY(ptest1, ptest2, ptest3) );
 
506
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
 
507
CREATE TABLE FKTABLE ( ftest1 int DEFAULT 0, ftest2 int, ftest3 int, ftest4 int,  CONSTRAINT constrname3
 
508
                        FOREIGN KEY(ftest1, ftest2, ftest3) REFERENCES PKTABLE
 
509
                        ON DELETE SET DEFAULT ON UPDATE SET NULL);
 
510
-- Insert Primary Key values
 
511
INSERT INTO PKTABLE VALUES (1, 2, 3, 'test1');
 
512
INSERT INTO PKTABLE VALUES (1, 3, 3, 'test2');
 
513
INSERT INTO PKTABLE VALUES (2, 3, 4, 'test3');
 
514
INSERT INTO PKTABLE VALUES (2, 4, 5, 'test4');
 
515
-- Insert Foreign Key values
 
516
INSERT INTO FKTABLE VALUES (1, 2, 3, 1); 
 
517
INSERT INTO FKTABLE VALUES (2, 3, 4, 1); 
 
518
INSERT INTO FKTABLE VALUES (NULL, 2, 3, 2);
 
519
INSERT INTO FKTABLE VALUES (2, NULL, 3, 3);
 
520
INSERT INTO FKTABLE VALUES (NULL, 2, 7, 4);
 
521
INSERT INTO FKTABLE VALUES (NULL, 3, 4, 5);
 
522
-- Insert a failed values
 
523
INSERT INTO FKTABLE VALUES (1, 2, 7, 6);
 
524
ERROR:  insert or update on table "fktable" violates foreign key constraint "constrname3"
 
525
DETAIL:  Key (ftest1,ftest2,ftest3)=(1,2,7) is not present in table "pktable".
 
526
-- Show FKTABLE
 
527
SELECT * from FKTABLE;
 
528
 ftest1 | ftest2 | ftest3 | ftest4 
 
529
--------+--------+--------+--------
 
530
      1 |      2 |      3 |      1
 
531
      2 |      3 |      4 |      1
 
532
        |      2 |      3 |      2
 
533
      2 |        |      3 |      3
 
534
        |      2 |      7 |      4
 
535
        |      3 |      4 |      5
 
536
(6 rows)
 
537
 
 
538
-- Try to update something that will set null
 
539
UPDATE PKTABLE set ptest2=5 where ptest2=2;
 
540
-- Try to update something that should not set null
 
541
UPDATE PKTABLE set ptest2=2 WHERE ptest2=3 and ptest1=1;
 
542
-- Show PKTABLE and FKTABLE
 
543
SELECT * from PKTABLE;
 
544
 ptest1 | ptest2 | ptest3 | ptest4 
 
545
--------+--------+--------+--------
 
546
      2 |      3 |      4 | test3
 
547
      2 |      4 |      5 | test4
 
548
      1 |      5 |      3 | test1
 
549
      1 |      2 |      3 | test2
 
550
(4 rows)
 
551
 
 
552
SELECT * from FKTABLE;
 
553
 ftest1 | ftest2 | ftest3 | ftest4 
 
554
--------+--------+--------+--------
 
555
      2 |      3 |      4 |      1
 
556
        |      2 |      3 |      2
 
557
      2 |        |      3 |      3
 
558
        |      2 |      7 |      4
 
559
        |      3 |      4 |      5
 
560
      1 |        |      3 |      1
 
561
(6 rows)
 
562
 
 
563
-- Try to delete something that should set default
 
564
DELETE FROM PKTABLE where ptest1=2 and ptest2=3 and ptest3=4;
 
565
-- Show PKTABLE and FKTABLE
 
566
SELECT * from PKTABLE;
 
567
 ptest1 | ptest2 | ptest3 | ptest4 
 
568
--------+--------+--------+--------
 
569
      2 |      4 |      5 | test4
 
570
      1 |      5 |      3 | test1
 
571
      1 |      2 |      3 | test2
 
572
(3 rows)
 
573
 
 
574
SELECT * from FKTABLE;
 
575
 ftest1 | ftest2 | ftest3 | ftest4 
 
576
--------+--------+--------+--------
 
577
        |      2 |      3 |      2
 
578
      2 |        |      3 |      3
 
579
        |      2 |      7 |      4
 
580
        |      3 |      4 |      5
 
581
      1 |        |      3 |      1
 
582
      0 |        |        |      1
 
583
(6 rows)
 
584
 
 
585
-- Try to delete something that should not set default
 
586
DELETE FROM PKTABLE where ptest2=5;
 
587
-- Show PKTABLE and FKTABLE
 
588
SELECT * from PKTABLE;
 
589
 ptest1 | ptest2 | ptest3 | ptest4 
 
590
--------+--------+--------+--------
 
591
      2 |      4 |      5 | test4
 
592
      1 |      2 |      3 | test2
 
593
(2 rows)
 
594
 
 
595
SELECT * from FKTABLE;
 
596
 ftest1 | ftest2 | ftest3 | ftest4 
 
597
--------+--------+--------+--------
 
598
        |      2 |      3 |      2
 
599
      2 |        |      3 |      3
 
600
        |      2 |      7 |      4
 
601
        |      3 |      4 |      5
 
602
      1 |        |      3 |      1
 
603
      0 |        |        |      1
 
604
(6 rows)
 
605
 
 
606
DROP TABLE FKTABLE;
 
607
DROP TABLE PKTABLE;
 
608
-- set default update / set null delete
 
609
CREATE TABLE PKTABLE ( ptest1 int, ptest2 int, ptest3 int, ptest4 text, PRIMARY KEY(ptest1, ptest2, ptest3) );
 
610
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
 
611
CREATE TABLE FKTABLE ( ftest1 int DEFAULT 0, ftest2 int DEFAULT -1, ftest3 int, ftest4 int,  CONSTRAINT constrname3
 
612
                        FOREIGN KEY(ftest1, ftest2, ftest3) REFERENCES PKTABLE
 
613
                        ON DELETE SET NULL ON UPDATE SET DEFAULT);
 
614
-- Insert Primary Key values
 
615
INSERT INTO PKTABLE VALUES (1, 2, 3, 'test1');
 
616
INSERT INTO PKTABLE VALUES (1, 3, 3, 'test2');
 
617
INSERT INTO PKTABLE VALUES (2, 3, 4, 'test3');
 
618
INSERT INTO PKTABLE VALUES (2, 4, 5, 'test4');
 
619
INSERT INTO PKTABLE VALUES (2, -1, 5, 'test5');
 
620
-- Insert Foreign Key values
 
621
INSERT INTO FKTABLE VALUES (1, 2, 3, 1); 
 
622
INSERT INTO FKTABLE VALUES (2, 3, 4, 1); 
 
623
INSERT INTO FKTABLE VALUES (2, 4, 5, 1);
 
624
INSERT INTO FKTABLE VALUES (NULL, 2, 3, 2);
 
625
INSERT INTO FKTABLE VALUES (2, NULL, 3, 3);
 
626
INSERT INTO FKTABLE VALUES (NULL, 2, 7, 4);
 
627
INSERT INTO FKTABLE VALUES (NULL, 3, 4, 5);
 
628
-- Insert a failed values
 
629
INSERT INTO FKTABLE VALUES (1, 2, 7, 6);
 
630
ERROR:  insert or update on table "fktable" violates foreign key constraint "constrname3"
 
631
DETAIL:  Key (ftest1,ftest2,ftest3)=(1,2,7) is not present in table "pktable".
 
632
-- Show FKTABLE
 
633
SELECT * from FKTABLE;
 
634
 ftest1 | ftest2 | ftest3 | ftest4 
 
635
--------+--------+--------+--------
 
636
      1 |      2 |      3 |      1
 
637
      2 |      3 |      4 |      1
 
638
      2 |      4 |      5 |      1
 
639
        |      2 |      3 |      2
 
640
      2 |        |      3 |      3
 
641
        |      2 |      7 |      4
 
642
        |      3 |      4 |      5
 
643
(7 rows)
 
644
 
 
645
-- Try to update something that will fail
 
646
UPDATE PKTABLE set ptest2=5 where ptest2=2;
 
647
ERROR:  insert or update on table "fktable" violates foreign key constraint "constrname3"
 
648
DETAIL:  Key (ftest1,ftest2,ftest3)=(1,-1,3) is not present in table "pktable".
 
649
-- Try to update something that will set default
 
650
UPDATE PKTABLE set ptest1=0, ptest2=5, ptest3=10 where ptest2=2;
 
651
UPDATE PKTABLE set ptest2=10 where ptest2=4;
 
652
-- Try to update something that should not set default
 
653
UPDATE PKTABLE set ptest2=2 WHERE ptest2=3 and ptest1=1;
 
654
-- Show PKTABLE and FKTABLE
 
655
SELECT * from PKTABLE;
 
656
 ptest1 | ptest2 | ptest3 | ptest4 
 
657
--------+--------+--------+--------
 
658
      2 |      3 |      4 | test3
 
659
      2 |     -1 |      5 | test5
 
660
      0 |      5 |     10 | test1
 
661
      2 |     10 |      5 | test4
 
662
      1 |      2 |      3 | test2
 
663
(5 rows)
 
664
 
 
665
SELECT * from FKTABLE;
 
666
 ftest1 | ftest2 | ftest3 | ftest4 
 
667
--------+--------+--------+--------
 
668
      2 |      3 |      4 |      1
 
669
        |      2 |      3 |      2
 
670
      2 |        |      3 |      3
 
671
        |      2 |      7 |      4
 
672
        |      3 |      4 |      5
 
673
      0 |     -1 |        |      1
 
674
      2 |     -1 |      5 |      1
 
675
(7 rows)
 
676
 
 
677
-- Try to delete something that should set null
 
678
DELETE FROM PKTABLE where ptest1=2 and ptest2=3 and ptest3=4;
 
679
-- Show PKTABLE and FKTABLE
 
680
SELECT * from PKTABLE;
 
681
 ptest1 | ptest2 | ptest3 | ptest4 
 
682
--------+--------+--------+--------
 
683
      2 |     -1 |      5 | test5
 
684
      0 |      5 |     10 | test1
 
685
      2 |     10 |      5 | test4
 
686
      1 |      2 |      3 | test2
 
687
(4 rows)
 
688
 
 
689
SELECT * from FKTABLE;
 
690
 ftest1 | ftest2 | ftest3 | ftest4 
 
691
--------+--------+--------+--------
 
692
        |      2 |      3 |      2
 
693
      2 |        |      3 |      3
 
694
        |      2 |      7 |      4
 
695
        |      3 |      4 |      5
 
696
      0 |     -1 |        |      1
 
697
      2 |     -1 |      5 |      1
 
698
        |        |        |      1
 
699
(7 rows)
 
700
 
 
701
-- Try to delete something that should not set null
 
702
DELETE FROM PKTABLE where ptest2=5;
 
703
-- Show PKTABLE and FKTABLE
 
704
SELECT * from PKTABLE;
 
705
 ptest1 | ptest2 | ptest3 | ptest4 
 
706
--------+--------+--------+--------
 
707
      2 |     -1 |      5 | test5
 
708
      2 |     10 |      5 | test4
 
709
      1 |      2 |      3 | test2
 
710
(3 rows)
 
711
 
 
712
SELECT * from FKTABLE;
 
713
 ftest1 | ftest2 | ftest3 | ftest4 
 
714
--------+--------+--------+--------
 
715
        |      2 |      3 |      2
 
716
      2 |        |      3 |      3
 
717
        |      2 |      7 |      4
 
718
        |      3 |      4 |      5
 
719
      0 |     -1 |        |      1
 
720
      2 |     -1 |      5 |      1
 
721
        |        |        |      1
 
722
(7 rows)
 
723
 
 
724
DROP TABLE FKTABLE;
 
725
DROP TABLE PKTABLE;
 
726
CREATE TABLE PKTABLE (ptest1 int PRIMARY KEY);
 
727
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
 
728
CREATE TABLE FKTABLE_FAIL1 ( ftest1 int, CONSTRAINT fkfail1 FOREIGN KEY (ftest2) REFERENCES PKTABLE);
 
729
ERROR:  column "ftest2" referenced in foreign key constraint does not exist
 
730
CREATE TABLE FKTABLE_FAIL2 ( ftest1 int, CONSTRAINT fkfail1 FOREIGN KEY (ftest1) REFERENCES PKTABLE(ptest2));
 
731
ERROR:  column "ptest2" referenced in foreign key constraint does not exist
 
732
DROP TABLE FKTABLE_FAIL1;
 
733
ERROR:  table "fktable_fail1" does not exist
 
734
DROP TABLE FKTABLE_FAIL2;
 
735
ERROR:  table "fktable_fail2" does not exist
 
736
DROP TABLE PKTABLE;
 
737
-- Test for referencing column number smaller than referenced constraint
 
738
CREATE TABLE PKTABLE (ptest1 int, ptest2 int, UNIQUE(ptest1, ptest2));
 
739
NOTICE:  CREATE TABLE / UNIQUE will create implicit index "pktable_ptest1_key" for table "pktable"
 
740
CREATE TABLE FKTABLE_FAIL1 (ftest1 int REFERENCES pktable(ptest1));
 
741
ERROR:  there is no unique constraint matching given keys for referenced table "pktable"
 
742
DROP TABLE FKTABLE_FAIL1;
 
743
ERROR:  table "fktable_fail1" does not exist
 
744
DROP TABLE PKTABLE;
 
745
--
 
746
-- Tests for mismatched types
 
747
--
 
748
-- Basic one column, two table setup 
 
749
CREATE TABLE PKTABLE (ptest1 int PRIMARY KEY);
 
750
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
 
751
INSERT INTO PKTABLE VALUES(42);
 
752
-- This next should fail, because int=inet does not exist
 
753
CREATE TABLE FKTABLE (ftest1 inet REFERENCES pktable);
 
754
ERROR:  foreign key constraint "fktable_ftest1_fkey" cannot be implemented
 
755
DETAIL:  Key columns "ftest1" and "ptest1" are of incompatible types: inet and integer.
 
756
-- This should also fail for the same reason, but here we
 
757
-- give the column name
 
758
CREATE TABLE FKTABLE (ftest1 inet REFERENCES pktable(ptest1));
 
759
ERROR:  foreign key constraint "fktable_ftest1_fkey" cannot be implemented
 
760
DETAIL:  Key columns "ftest1" and "ptest1" are of incompatible types: inet and integer.
 
761
-- This should succeed, even though they are different types,
 
762
-- because int=int8 exists and is a member of the integer opfamily
 
763
CREATE TABLE FKTABLE (ftest1 int8 REFERENCES pktable);
 
764
-- Check it actually works
 
765
INSERT INTO FKTABLE VALUES(42);         -- should succeed
 
766
INSERT INTO FKTABLE VALUES(43);         -- should fail
 
767
ERROR:  insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey"
 
768
DETAIL:  Key (ftest1)=(43) is not present in table "pktable".
 
769
UPDATE FKTABLE SET ftest1 = ftest1;     -- should succeed
 
770
UPDATE FKTABLE SET ftest1 = ftest1 + 1; -- should fail
 
771
ERROR:  insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey"
 
772
DETAIL:  Key (ftest1)=(43) is not present in table "pktable".
 
773
DROP TABLE FKTABLE;
 
774
-- This should fail, because we'd have to cast numeric to int which is
 
775
-- not an implicit coercion (or use numeric=numeric, but that's not part
 
776
-- of the integer opfamily)
 
777
CREATE TABLE FKTABLE (ftest1 numeric REFERENCES pktable);
 
778
ERROR:  foreign key constraint "fktable_ftest1_fkey" cannot be implemented
 
779
DETAIL:  Key columns "ftest1" and "ptest1" are of incompatible types: numeric and integer.
 
780
DROP TABLE PKTABLE;
 
781
-- On the other hand, this should work because int implicitly promotes to
 
782
-- numeric, and we allow promotion on the FK side
 
783
CREATE TABLE PKTABLE (ptest1 numeric PRIMARY KEY);
 
784
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
 
785
INSERT INTO PKTABLE VALUES(42);
 
786
CREATE TABLE FKTABLE (ftest1 int REFERENCES pktable);
 
787
-- Check it actually works
 
788
INSERT INTO FKTABLE VALUES(42);         -- should succeed
 
789
INSERT INTO FKTABLE VALUES(43);         -- should fail
 
790
ERROR:  insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey"
 
791
DETAIL:  Key (ftest1)=(43) is not present in table "pktable".
 
792
UPDATE FKTABLE SET ftest1 = ftest1;     -- should succeed
 
793
UPDATE FKTABLE SET ftest1 = ftest1 + 1; -- should fail
 
794
ERROR:  insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey"
 
795
DETAIL:  Key (ftest1)=(43) is not present in table "pktable".
 
796
DROP TABLE FKTABLE;
 
797
DROP TABLE PKTABLE;
 
798
-- Two columns, two tables
 
799
CREATE TABLE PKTABLE (ptest1 int, ptest2 inet, PRIMARY KEY(ptest1, ptest2));
 
800
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
 
801
-- This should fail, because we just chose really odd types
 
802
CREATE TABLE FKTABLE (ftest1 cidr, ftest2 timestamp, FOREIGN KEY(ftest1, ftest2) REFERENCES pktable);
 
803
ERROR:  foreign key constraint "fktable_ftest1_fkey" cannot be implemented
 
804
DETAIL:  Key columns "ftest1" and "ptest1" are of incompatible types: cidr and integer.
 
805
-- Again, so should this...
 
806
CREATE TABLE FKTABLE (ftest1 cidr, ftest2 timestamp, FOREIGN KEY(ftest1, ftest2) REFERENCES pktable(ptest1, ptest2));
 
807
ERROR:  foreign key constraint "fktable_ftest1_fkey" cannot be implemented
 
808
DETAIL:  Key columns "ftest1" and "ptest1" are of incompatible types: cidr and integer.
 
809
-- This fails because we mixed up the column ordering
 
810
CREATE TABLE FKTABLE (ftest1 int, ftest2 inet, FOREIGN KEY(ftest2, ftest1) REFERENCES pktable);
 
811
ERROR:  foreign key constraint "fktable_ftest2_fkey" cannot be implemented
 
812
DETAIL:  Key columns "ftest2" and "ptest1" are of incompatible types: inet and integer.
 
813
-- As does this...
 
814
CREATE TABLE FKTABLE (ftest1 int, ftest2 inet, FOREIGN KEY(ftest2, ftest1) REFERENCES pktable(ptest1, ptest2));
 
815
ERROR:  foreign key constraint "fktable_ftest2_fkey" cannot be implemented
 
816
DETAIL:  Key columns "ftest2" and "ptest1" are of incompatible types: inet and integer.
 
817
-- And again..
 
818
CREATE TABLE FKTABLE (ftest1 int, ftest2 inet, FOREIGN KEY(ftest1, ftest2) REFERENCES pktable(ptest2, ptest1));
 
819
ERROR:  foreign key constraint "fktable_ftest1_fkey" cannot be implemented
 
820
DETAIL:  Key columns "ftest1" and "ptest2" are of incompatible types: integer and inet.
 
821
-- This works...
 
822
CREATE TABLE FKTABLE (ftest1 int, ftest2 inet, FOREIGN KEY(ftest2, ftest1) REFERENCES pktable(ptest2, ptest1));
 
823
DROP TABLE FKTABLE;
 
824
-- As does this
 
825
CREATE TABLE FKTABLE (ftest1 int, ftest2 inet, FOREIGN KEY(ftest1, ftest2) REFERENCES pktable(ptest1, ptest2));
 
826
DROP TABLE FKTABLE;
 
827
DROP TABLE PKTABLE;
 
828
-- Two columns, same table
 
829
-- Make sure this still works...
 
830
CREATE TABLE PKTABLE (ptest1 int, ptest2 inet, ptest3 int, ptest4 inet, PRIMARY KEY(ptest1, ptest2), FOREIGN KEY(ptest3,
 
831
ptest4) REFERENCES pktable(ptest1, ptest2));
 
832
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
 
833
DROP TABLE PKTABLE;
 
834
-- And this, 
 
835
CREATE TABLE PKTABLE (ptest1 int, ptest2 inet, ptest3 int, ptest4 inet, PRIMARY KEY(ptest1, ptest2), FOREIGN KEY(ptest3,
 
836
ptest4) REFERENCES pktable);
 
837
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
 
838
DROP TABLE PKTABLE;
 
839
-- This shouldn't (mixed up columns)
 
840
CREATE TABLE PKTABLE (ptest1 int, ptest2 inet, ptest3 int, ptest4 inet, PRIMARY KEY(ptest1, ptest2), FOREIGN KEY(ptest3,
 
841
ptest4) REFERENCES pktable(ptest2, ptest1));
 
842
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
 
843
ERROR:  foreign key constraint "pktable_ptest3_fkey" cannot be implemented
 
844
DETAIL:  Key columns "ptest3" and "ptest2" are of incompatible types: integer and inet.
 
845
-- Nor should this... (same reason, we have 4,3 referencing 1,2 which mismatches types
 
846
CREATE TABLE PKTABLE (ptest1 int, ptest2 inet, ptest3 int, ptest4 inet, PRIMARY KEY(ptest1, ptest2), FOREIGN KEY(ptest4,
 
847
ptest3) REFERENCES pktable(ptest1, ptest2));
 
848
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
 
849
ERROR:  foreign key constraint "pktable_ptest4_fkey" cannot be implemented
 
850
DETAIL:  Key columns "ptest4" and "ptest1" are of incompatible types: inet and integer.
 
851
-- Not this one either... Same as the last one except we didn't defined the columns being referenced.
 
852
CREATE TABLE PKTABLE (ptest1 int, ptest2 inet, ptest3 int, ptest4 inet, PRIMARY KEY(ptest1, ptest2), FOREIGN KEY(ptest4,
 
853
ptest3) REFERENCES pktable);
 
854
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
 
855
ERROR:  foreign key constraint "pktable_ptest4_fkey" cannot be implemented
 
856
DETAIL:  Key columns "ptest4" and "ptest1" are of incompatible types: inet and integer.
 
857
--
 
858
-- Now some cases with inheritance
 
859
-- Basic 2 table case: 1 column of matching types.
 
860
create table pktable_base (base1 int not null);
 
861
create table pktable (ptest1 int, primary key(base1), unique(base1, ptest1)) inherits (pktable_base);
 
862
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
 
863
NOTICE:  CREATE TABLE / UNIQUE will create implicit index "pktable_base1_key" for table "pktable"
 
864
create table fktable (ftest1 int references pktable(base1));
 
865
-- now some ins, upd, del
 
866
insert into pktable(base1) values (1);
 
867
insert into pktable(base1) values (2);
 
868
--  let's insert a non-existant fktable value
 
869
insert into fktable(ftest1) values (3);
 
870
ERROR:  insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey"
 
871
DETAIL:  Key (ftest1)=(3) is not present in table "pktable".
 
872
--  let's make a valid row for that
 
873
insert into pktable(base1) values (3);
 
874
insert into fktable(ftest1) values (3);
 
875
-- let's try removing a row that should fail from pktable
 
876
delete from pktable where base1>2;
 
877
ERROR:  update or delete on table "pktable" violates foreign key constraint "fktable_ftest1_fkey" on table "fktable"
 
878
DETAIL:  Key (base1)=(3) is still referenced from table "fktable".
 
879
-- okay, let's try updating all of the base1 values to *4
 
880
-- which should fail.
 
881
update pktable set base1=base1*4;
 
882
ERROR:  update or delete on table "pktable" violates foreign key constraint "fktable_ftest1_fkey" on table "fktable"
 
883
DETAIL:  Key (base1)=(3) is still referenced from table "fktable".
 
884
-- okay, let's try an update that should work.
 
885
update pktable set base1=base1*4 where base1<3;
 
886
-- and a delete that should work
 
887
delete from pktable where base1>3;
 
888
-- cleanup
 
889
drop table fktable;
 
890
delete from pktable;
 
891
-- Now 2 columns 2 tables, matching types
 
892
create table fktable (ftest1 int, ftest2 int, foreign key(ftest1, ftest2) references pktable(base1, ptest1));
 
893
-- now some ins, upd, del
 
894
insert into pktable(base1, ptest1) values (1, 1);
 
895
insert into pktable(base1, ptest1) values (2, 2);
 
896
--  let's insert a non-existant fktable value
 
897
insert into fktable(ftest1, ftest2) values (3, 1);
 
898
ERROR:  insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey"
 
899
DETAIL:  Key (ftest1,ftest2)=(3,1) is not present in table "pktable".
 
900
--  let's make a valid row for that
 
901
insert into pktable(base1,ptest1) values (3, 1);
 
902
insert into fktable(ftest1, ftest2) values (3, 1);
 
903
-- let's try removing a row that should fail from pktable
 
904
delete from pktable where base1>2;
 
905
ERROR:  update or delete on table "pktable" violates foreign key constraint "fktable_ftest1_fkey" on table "fktable"
 
906
DETAIL:  Key (base1,ptest1)=(3,1) is still referenced from table "fktable".
 
907
-- okay, let's try updating all of the base1 values to *4
 
908
-- which should fail.
 
909
update pktable set base1=base1*4;
 
910
ERROR:  update or delete on table "pktable" violates foreign key constraint "fktable_ftest1_fkey" on table "fktable"
 
911
DETAIL:  Key (base1,ptest1)=(3,1) is still referenced from table "fktable".
 
912
-- okay, let's try an update that should work.
 
913
update pktable set base1=base1*4 where base1<3;
 
914
-- and a delete that should work
 
915
delete from pktable where base1>3;
 
916
-- cleanup
 
917
drop table fktable;
 
918
drop table pktable;
 
919
drop table pktable_base;
 
920
-- Now we'll do one all in 1 table with 2 columns of matching types
 
921
create table pktable_base(base1 int not null, base2 int);
 
922
create table pktable(ptest1 int, ptest2 int, primary key(base1, ptest1), foreign key(base2, ptest2) references
 
923
                                             pktable(base1, ptest1)) inherits (pktable_base);
 
924
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
 
925
insert into pktable (base1, ptest1, base2, ptest2) values (1, 1, 1, 1);
 
926
insert into pktable (base1, ptest1, base2, ptest2) values (2, 1, 1, 1);
 
927
insert into pktable (base1, ptest1, base2, ptest2) values (2, 2, 2, 1);
 
928
insert into pktable (base1, ptest1, base2, ptest2) values (1, 3, 2, 2);
 
929
-- fails (3,2) isn't in base1, ptest1
 
930
insert into pktable (base1, ptest1, base2, ptest2) values (2, 3, 3, 2);
 
931
ERROR:  insert or update on table "pktable" violates foreign key constraint "pktable_base2_fkey"
 
932
DETAIL:  Key (base2,ptest2)=(3,2) is not present in table "pktable".
 
933
-- fails (2,2) is being referenced
 
934
delete from pktable where base1=2;
 
935
ERROR:  update or delete on table "pktable" violates foreign key constraint "pktable_base2_fkey" on table "pktable"
 
936
DETAIL:  Key (base1,ptest1)=(2,2) is still referenced from table "pktable".
 
937
-- fails (1,1) is being referenced (twice)
 
938
update pktable set base1=3 where base1=1;
 
939
ERROR:  update or delete on table "pktable" violates foreign key constraint "pktable_base2_fkey" on table "pktable"
 
940
DETAIL:  Key (base1,ptest1)=(1,1) is still referenced from table "pktable".
 
941
-- this sequence of two deletes will work, since after the first there will be no (2,*) references
 
942
delete from pktable where base2=2;
 
943
delete from pktable where base1=2;
 
944
drop table pktable;
 
945
drop table pktable_base;
 
946
-- 2 columns (2 tables), mismatched types
 
947
create table pktable_base(base1 int not null);
 
948
create table pktable(ptest1 inet, primary key(base1, ptest1)) inherits (pktable_base);
 
949
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
 
950
-- just generally bad types (with and without column references on the referenced table)
 
951
create table fktable(ftest1 cidr, ftest2 int[], foreign key (ftest1, ftest2) references pktable);
 
952
ERROR:  foreign key constraint "fktable_ftest1_fkey" cannot be implemented
 
953
DETAIL:  Key columns "ftest1" and "base1" are of incompatible types: cidr and integer.
 
954
create table fktable(ftest1 cidr, ftest2 int[], foreign key (ftest1, ftest2) references pktable(base1, ptest1));
 
955
ERROR:  foreign key constraint "fktable_ftest1_fkey" cannot be implemented
 
956
DETAIL:  Key columns "ftest1" and "base1" are of incompatible types: cidr and integer.
 
957
-- let's mix up which columns reference which
 
958
create table fktable(ftest1 int, ftest2 inet, foreign key(ftest2, ftest1) references pktable);
 
959
ERROR:  foreign key constraint "fktable_ftest2_fkey" cannot be implemented
 
960
DETAIL:  Key columns "ftest2" and "base1" are of incompatible types: inet and integer.
 
961
create table fktable(ftest1 int, ftest2 inet, foreign key(ftest2, ftest1) references pktable(base1, ptest1));
 
962
ERROR:  foreign key constraint "fktable_ftest2_fkey" cannot be implemented
 
963
DETAIL:  Key columns "ftest2" and "base1" are of incompatible types: inet and integer.
 
964
create table fktable(ftest1 int, ftest2 inet, foreign key(ftest1, ftest2) references pktable(ptest1, base1));
 
965
ERROR:  foreign key constraint "fktable_ftest1_fkey" cannot be implemented
 
966
DETAIL:  Key columns "ftest1" and "ptest1" are of incompatible types: integer and inet.
 
967
drop table pktable;
 
968
drop table pktable_base;
 
969
-- 2 columns (1 table), mismatched types
 
970
create table pktable_base(base1 int not null, base2 int);
 
971
create table pktable(ptest1 inet, ptest2 inet[], primary key(base1, ptest1), foreign key(base2, ptest2) references
 
972
                                             pktable(base1, ptest1)) inherits (pktable_base);
 
973
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
 
974
ERROR:  foreign key constraint "pktable_base2_fkey" cannot be implemented
 
975
DETAIL:  Key columns "ptest2" and "ptest1" are of incompatible types: inet[] and inet.
 
976
create table pktable(ptest1 inet, ptest2 inet, primary key(base1, ptest1), foreign key(base2, ptest2) references
 
977
                                             pktable(ptest1, base1)) inherits (pktable_base);
 
978
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
 
979
ERROR:  foreign key constraint "pktable_base2_fkey" cannot be implemented
 
980
DETAIL:  Key columns "base2" and "ptest1" are of incompatible types: integer and inet.
 
981
create table pktable(ptest1 inet, ptest2 inet, primary key(base1, ptest1), foreign key(ptest2, base2) references
 
982
                                             pktable(base1, ptest1)) inherits (pktable_base);
 
983
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
 
984
ERROR:  foreign key constraint "pktable_ptest2_fkey" cannot be implemented
 
985
DETAIL:  Key columns "ptest2" and "base1" are of incompatible types: inet and integer.
 
986
create table pktable(ptest1 inet, ptest2 inet, primary key(base1, ptest1), foreign key(ptest2, base2) references
 
987
                                             pktable(base1, ptest1)) inherits (pktable_base);
 
988
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
 
989
ERROR:  foreign key constraint "pktable_ptest2_fkey" cannot be implemented
 
990
DETAIL:  Key columns "ptest2" and "base1" are of incompatible types: inet and integer.
 
991
drop table pktable;
 
992
ERROR:  table "pktable" does not exist
 
993
drop table pktable_base;
 
994
--
 
995
-- Deferrable constraints
 
996
--              (right now, only FOREIGN KEY constraints can be deferred)
 
997
--
 
998
-- deferrable, explicitly deferred
 
999
CREATE TABLE pktable (
 
1000
        id              INT4 PRIMARY KEY,
 
1001
        other   INT4
 
1002
);
 
1003
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
 
1004
CREATE TABLE fktable (
 
1005
        id              INT4 PRIMARY KEY,
 
1006
        fk              INT4 REFERENCES pktable DEFERRABLE
 
1007
);
 
1008
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "fktable_pkey" for table "fktable"
 
1009
-- default to immediate: should fail
 
1010
INSERT INTO fktable VALUES (5, 10);
 
1011
ERROR:  insert or update on table "fktable" violates foreign key constraint "fktable_fk_fkey"
 
1012
DETAIL:  Key (fk)=(10) is not present in table "pktable".
 
1013
-- explicitly defer the constraint
 
1014
BEGIN;
 
1015
SET CONSTRAINTS ALL DEFERRED;
 
1016
INSERT INTO fktable VALUES (10, 15);
 
1017
INSERT INTO pktable VALUES (15, 0); -- make the FK insert valid
 
1018
COMMIT;
 
1019
DROP TABLE fktable, pktable;
 
1020
-- deferrable, initially deferred
 
1021
CREATE TABLE pktable (
 
1022
        id              INT4 PRIMARY KEY,
 
1023
        other   INT4
 
1024
);
 
1025
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
 
1026
CREATE TABLE fktable (
 
1027
        id              INT4 PRIMARY KEY,
 
1028
        fk              INT4 REFERENCES pktable DEFERRABLE INITIALLY DEFERRED
 
1029
);
 
1030
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "fktable_pkey" for table "fktable"
 
1031
-- default to deferred, should succeed
 
1032
BEGIN;
 
1033
INSERT INTO fktable VALUES (100, 200);
 
1034
INSERT INTO pktable VALUES (200, 500); -- make the FK insert valid
 
1035
COMMIT;
 
1036
-- default to deferred, explicitly make immediate
 
1037
BEGIN;
 
1038
SET CONSTRAINTS ALL IMMEDIATE;
 
1039
-- should fail
 
1040
INSERT INTO fktable VALUES (500, 1000);
 
1041
ERROR:  insert or update on table "fktable" violates foreign key constraint "fktable_fk_fkey"
 
1042
DETAIL:  Key (fk)=(1000) is not present in table "pktable".
 
1043
COMMIT;
 
1044
DROP TABLE fktable, pktable;
 
1045
-- tricky behavior: according to SQL99, if a deferred constraint is set
 
1046
-- to 'immediate' mode, it should be checked for validity *immediately*,
 
1047
-- not when the current transaction commits (i.e. the mode change applies
 
1048
-- retroactively)
 
1049
CREATE TABLE pktable (
 
1050
        id              INT4 PRIMARY KEY,
 
1051
        other   INT4
 
1052
);
 
1053
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
 
1054
CREATE TABLE fktable (
 
1055
        id              INT4 PRIMARY KEY,
 
1056
        fk              INT4 REFERENCES pktable DEFERRABLE
 
1057
);
 
1058
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "fktable_pkey" for table "fktable"
 
1059
BEGIN;
 
1060
SET CONSTRAINTS ALL DEFERRED;
 
1061
-- should succeed, for now
 
1062
INSERT INTO fktable VALUES (1000, 2000);
 
1063
-- should cause transaction abort, due to preceding error
 
1064
SET CONSTRAINTS ALL IMMEDIATE;
 
1065
ERROR:  insert or update on table "fktable" violates foreign key constraint "fktable_fk_fkey"
 
1066
DETAIL:  Key (fk)=(2000) is not present in table "pktable".
 
1067
INSERT INTO pktable VALUES (2000, 3); -- too late
 
1068
ERROR:  current transaction is aborted, commands ignored until end of transaction block
 
1069
COMMIT;
 
1070
DROP TABLE fktable, pktable;
 
1071
-- deferrable, initially deferred
 
1072
CREATE TABLE pktable (
 
1073
        id              INT4 PRIMARY KEY,
 
1074
        other   INT4
 
1075
);
 
1076
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
 
1077
CREATE TABLE fktable (
 
1078
        id              INT4 PRIMARY KEY,
 
1079
        fk              INT4 REFERENCES pktable DEFERRABLE INITIALLY DEFERRED
 
1080
);
 
1081
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "fktable_pkey" for table "fktable"
 
1082
BEGIN;
 
1083
-- no error here
 
1084
INSERT INTO fktable VALUES (100, 200);
 
1085
-- error here on commit
 
1086
COMMIT;
 
1087
ERROR:  insert or update on table "fktable" violates foreign key constraint "fktable_fk_fkey"
 
1088
DETAIL:  Key (fk)=(200) is not present in table "pktable".
 
1089
DROP TABLE pktable, fktable;
 
1090
-- test notice about expensive referential integrity checks,
 
1091
-- where the index cannot be used because of type incompatibilities.
 
1092
CREATE TEMP TABLE pktable (
 
1093
        id1     INT4 PRIMARY KEY,
 
1094
        id2     VARCHAR(4) UNIQUE,
 
1095
        id3     REAL UNIQUE,
 
1096
        UNIQUE(id1, id2, id3)
 
1097
);
 
1098
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
 
1099
NOTICE:  CREATE TABLE / UNIQUE will create implicit index "pktable_id2_key" for table "pktable"
 
1100
NOTICE:  CREATE TABLE / UNIQUE will create implicit index "pktable_id3_key" for table "pktable"
 
1101
NOTICE:  CREATE TABLE / UNIQUE will create implicit index "pktable_id1_key" for table "pktable"
 
1102
CREATE TEMP TABLE fktable (
 
1103
        x1      INT4 REFERENCES pktable(id1),
 
1104
        x2      VARCHAR(4) REFERENCES pktable(id2),
 
1105
        x3      REAL REFERENCES pktable(id3),
 
1106
        x4      TEXT,
 
1107
        x5      INT2
 
1108
);
 
1109
-- check individual constraints with alter table.
 
1110
-- should fail
 
1111
-- varchar does not promote to real
 
1112
ALTER TABLE fktable ADD CONSTRAINT fk_2_3
 
1113
FOREIGN KEY (x2) REFERENCES pktable(id3);
 
1114
ERROR:  foreign key constraint "fk_2_3" cannot be implemented
 
1115
DETAIL:  Key columns "x2" and "id3" are of incompatible types: character varying and real.
 
1116
-- nor to int4
 
1117
ALTER TABLE fktable ADD CONSTRAINT fk_2_1
 
1118
FOREIGN KEY (x2) REFERENCES pktable(id1);
 
1119
ERROR:  foreign key constraint "fk_2_1" cannot be implemented
 
1120
DETAIL:  Key columns "x2" and "id1" are of incompatible types: character varying and integer.
 
1121
-- real does not promote to int4
 
1122
ALTER TABLE fktable ADD CONSTRAINT fk_3_1
 
1123
FOREIGN KEY (x3) REFERENCES pktable(id1);
 
1124
ERROR:  foreign key constraint "fk_3_1" cannot be implemented
 
1125
DETAIL:  Key columns "x3" and "id1" are of incompatible types: real and integer.
 
1126
-- int4 does not promote to text
 
1127
ALTER TABLE fktable ADD CONSTRAINT fk_1_2
 
1128
FOREIGN KEY (x1) REFERENCES pktable(id2);
 
1129
ERROR:  foreign key constraint "fk_1_2" cannot be implemented
 
1130
DETAIL:  Key columns "x1" and "id2" are of incompatible types: integer and character varying.
 
1131
-- should succeed
 
1132
-- int4 promotes to real
 
1133
ALTER TABLE fktable ADD CONSTRAINT fk_1_3
 
1134
FOREIGN KEY (x1) REFERENCES pktable(id3);
 
1135
-- text is compatible with varchar
 
1136
ALTER TABLE fktable ADD CONSTRAINT fk_4_2
 
1137
FOREIGN KEY (x4) REFERENCES pktable(id2);
 
1138
-- int2 is part of integer opfamily as of 8.0
 
1139
ALTER TABLE fktable ADD CONSTRAINT fk_5_1
 
1140
FOREIGN KEY (x5) REFERENCES pktable(id1);
 
1141
-- check multikey cases, especially out-of-order column lists
 
1142
-- these should work
 
1143
ALTER TABLE fktable ADD CONSTRAINT fk_123_123
 
1144
FOREIGN KEY (x1,x2,x3) REFERENCES pktable(id1,id2,id3);
 
1145
ALTER TABLE fktable ADD CONSTRAINT fk_213_213
 
1146
FOREIGN KEY (x2,x1,x3) REFERENCES pktable(id2,id1,id3);
 
1147
ALTER TABLE fktable ADD CONSTRAINT fk_253_213
 
1148
FOREIGN KEY (x2,x5,x3) REFERENCES pktable(id2,id1,id3);
 
1149
-- these should fail
 
1150
ALTER TABLE fktable ADD CONSTRAINT fk_123_231
 
1151
FOREIGN KEY (x1,x2,x3) REFERENCES pktable(id2,id3,id1);
 
1152
ERROR:  foreign key constraint "fk_123_231" cannot be implemented
 
1153
DETAIL:  Key columns "x1" and "id2" are of incompatible types: integer and character varying.
 
1154
ALTER TABLE fktable ADD CONSTRAINT fk_241_132
 
1155
FOREIGN KEY (x2,x4,x1) REFERENCES pktable(id1,id3,id2);
 
1156
ERROR:  foreign key constraint "fk_241_132" cannot be implemented
 
1157
DETAIL:  Key columns "x2" and "id1" are of incompatible types: character varying and integer.
 
1158
DROP TABLE pktable, fktable;
 
1159
-- test a tricky case: we can elide firing the FK check trigger during
 
1160
-- an UPDATE if the UPDATE did not change the foreign key
 
1161
-- field. However, we can't do this if our transaction was the one that
 
1162
-- created the updated row and the trigger is deferred, since our UPDATE
 
1163
-- will have invalidated the original newly-inserted tuple, and therefore
 
1164
-- cause the on-INSERT RI trigger not to be fired.
 
1165
CREATE TEMP TABLE pktable (
 
1166
    id int primary key,
 
1167
    other int
 
1168
);
 
1169
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
 
1170
CREATE TEMP TABLE fktable (
 
1171
    id int primary key,
 
1172
    fk int references pktable deferrable initially deferred
 
1173
);
 
1174
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "fktable_pkey" for table "fktable"
 
1175
INSERT INTO pktable VALUES (5, 10);
 
1176
BEGIN;
 
1177
-- doesn't match PK, but no error yet
 
1178
INSERT INTO fktable VALUES (0, 20);
 
1179
-- don't change FK
 
1180
UPDATE fktable SET id = id + 1;
 
1181
-- should catch error from initial INSERT
 
1182
COMMIT;
 
1183
ERROR:  insert or update on table "fktable" violates foreign key constraint "fktable_fk_fkey"
 
1184
DETAIL:  Key (fk)=(20) is not present in table "pktable".
 
1185
-- check same case when insert is in a different subtransaction than update
 
1186
BEGIN;
 
1187
-- doesn't match PK, but no error yet
 
1188
INSERT INTO fktable VALUES (0, 20);
 
1189
-- UPDATE will be in a subxact
 
1190
SAVEPOINT savept1;
 
1191
-- don't change FK
 
1192
UPDATE fktable SET id = id + 1;
 
1193
-- should catch error from initial INSERT
 
1194
COMMIT;
 
1195
ERROR:  insert or update on table "fktable" violates foreign key constraint "fktable_fk_fkey"
 
1196
DETAIL:  Key (fk)=(20) is not present in table "pktable".
 
1197
BEGIN;
 
1198
-- INSERT will be in a subxact
 
1199
SAVEPOINT savept1;
 
1200
-- doesn't match PK, but no error yet
 
1201
INSERT INTO fktable VALUES (0, 20);
 
1202
RELEASE SAVEPOINT savept1;
 
1203
-- don't change FK
 
1204
UPDATE fktable SET id = id + 1;
 
1205
-- should catch error from initial INSERT
 
1206
COMMIT;
 
1207
ERROR:  insert or update on table "fktable" violates foreign key constraint "fktable_fk_fkey"
 
1208
DETAIL:  Key (fk)=(20) is not present in table "pktable".
 
1209
BEGIN;
 
1210
-- doesn't match PK, but no error yet
 
1211
INSERT INTO fktable VALUES (0, 20);
 
1212
-- UPDATE will be in a subxact
 
1213
SAVEPOINT savept1;
 
1214
-- don't change FK
 
1215
UPDATE fktable SET id = id + 1;
 
1216
-- Roll back the UPDATE
 
1217
ROLLBACK TO savept1;
 
1218
-- should catch error from initial INSERT
 
1219
COMMIT;
 
1220
ERROR:  insert or update on table "fktable" violates foreign key constraint "fktable_fk_fkey"
 
1221
DETAIL:  Key (fk)=(20) is not present in table "pktable".
 
1222
-- test order of firing of FK triggers when several RI-induced changes need to
 
1223
-- be made to the same row.  This was broken by subtransaction-related
 
1224
-- changes in 8.0.
 
1225
CREATE TEMP TABLE users (
 
1226
  id INT PRIMARY KEY,
 
1227
  name VARCHAR NOT NULL
 
1228
);
 
1229
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "users_pkey" for table "users"
 
1230
INSERT INTO users VALUES (1, 'Jozko');
 
1231
INSERT INTO users VALUES (2, 'Ferko');
 
1232
INSERT INTO users VALUES (3, 'Samko');
 
1233
CREATE TEMP TABLE tasks (
 
1234
  id INT PRIMARY KEY,
 
1235
  owner INT REFERENCES users ON UPDATE CASCADE ON DELETE SET NULL,
 
1236
  worker INT REFERENCES users ON UPDATE CASCADE ON DELETE SET NULL,
 
1237
  checked_by INT REFERENCES users ON UPDATE CASCADE ON DELETE SET NULL
 
1238
);
 
1239
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "tasks_pkey" for table "tasks"
 
1240
INSERT INTO tasks VALUES (1,1,NULL,NULL);
 
1241
INSERT INTO tasks VALUES (2,2,2,NULL);
 
1242
INSERT INTO tasks VALUES (3,3,3,3);
 
1243
SELECT * FROM tasks;
 
1244
 id | owner | worker | checked_by 
 
1245
----+-------+--------+------------
 
1246
  1 |     1 |        |           
 
1247
  2 |     2 |      2 |           
 
1248
  3 |     3 |      3 |          3
 
1249
(3 rows)
 
1250
 
 
1251
UPDATE users SET id = 4 WHERE id = 3;
 
1252
SELECT * FROM tasks;
 
1253
 id | owner | worker | checked_by 
 
1254
----+-------+--------+------------
 
1255
  1 |     1 |        |           
 
1256
  2 |     2 |      2 |           
 
1257
  3 |     4 |      4 |          4
 
1258
(3 rows)
 
1259
 
 
1260
DELETE FROM users WHERE id = 4;
 
1261
SELECT * FROM tasks;
 
1262
 id | owner | worker | checked_by 
 
1263
----+-------+--------+------------
 
1264
  1 |     1 |        |           
 
1265
  2 |     2 |      2 |           
 
1266
  3 |       |        |           
 
1267
(3 rows)
 
1268
 
 
1269
-- could fail with only 2 changes to make, if row was already updated
 
1270
BEGIN;
 
1271
UPDATE tasks set id=id WHERE id=2;
 
1272
SELECT * FROM tasks;
 
1273
 id | owner | worker | checked_by 
 
1274
----+-------+--------+------------
 
1275
  1 |     1 |        |           
 
1276
  3 |       |        |           
 
1277
  2 |     2 |      2 |           
 
1278
(3 rows)
 
1279
 
 
1280
DELETE FROM users WHERE id = 2;
 
1281
SELECT * FROM tasks;
 
1282
 id | owner | worker | checked_by 
 
1283
----+-------+--------+------------
 
1284
  1 |     1 |        |           
 
1285
  3 |       |        |           
 
1286
  2 |       |        |           
 
1287
(3 rows)
 
1288
 
 
1289
COMMIT;