-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
fk
1997 lines (1492 loc) · 61.1 KB
/
fk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# LogicTest: local local-opt local-parallel-stmts fakedist fakedist-opt fakedist-metadata
statement ok
CREATE TABLE customers (id INT PRIMARY KEY, email STRING UNIQUE)
statement ok
INSERT INTO customers VALUES (1, '[email protected]'), (2, '[email protected]')
statement ok
CREATE TABLE products (sku STRING PRIMARY KEY, upc STRING UNIQUE, vendor STRING)
statement ok
INSERT INTO products VALUES ('VP-W9QH-W44L', '867072000006', 'Dave'), ('780', '885155001450', 'iRobot')
statement error pgcode 42P01 relation "productz" does not exist
CREATE TABLE missing (product STRING REFERENCES productz)
statement error pgcode 42P01 relation "customerz" does not exist
CREATE TABLE missing_with_col (customer INT REFERENCES customerz (id))
statement error pgcode 42703 column "idz" does not exist
CREATE TABLE missing_col (customer INT REFERENCES customers (idz))
statement ok
CREATE TABLE unindexed (customer INT REFERENCES customers)
query TTBITTBB colnames
SHOW INDEXES FROM unindexed
----
table_name index_name non_unique seq_in_index column_name direction storing implicit
unindexed primary false 1 rowid ASC false false
unindexed unindexed_auto_index_fk_customer_ref_customers true 1 customer ASC false false
unindexed unindexed_auto_index_fk_customer_ref_customers true 2 rowid ASC false true
statement error there is no unique constraint matching given keys for referenced table products
CREATE TABLE non_unique (product STRING REFERENCES products (vendor))
statement error type of "customer" \(INT\) does not match foreign key "customers"."email" \(STRING\)
CREATE TABLE mismatch (customer INT REFERENCES customers (email))
statement error columns cannot be used by multiple foreign key constraints
CREATE TABLE orders (
id INT PRIMARY KEY,
product STRING REFERENCES products,
customer INT CONSTRAINT valid_customer REFERENCES customers (id),
CONSTRAINT fk FOREIGN KEY (product) REFERENCES products,
INDEX (product),
INDEX (customer)
)
statement ok
CREATE TABLE orders (
id INT,
shipment INT,
product STRING DEFAULT 'sprockets' REFERENCES products,
customer INT CONSTRAINT valid_customer REFERENCES customers (id),
PRIMARY KEY (id, shipment),
INDEX (product),
INDEX (customer)
)
statement ok
ALTER TABLE orders DROP CONSTRAINT fk_product_ref_products
statement ok
ALTER TABLE orders ADD FOREIGN KEY (product) REFERENCES products ON DELETE NO ACTION
statement error pq: columns cannot be used by multiple foreign key constraints
ALTER TABLE orders ADD FOREIGN KEY (product) REFERENCES products ON UPDATE NO ACTION
statement ok
ALTER TABLE orders DROP CONSTRAINT fk_product_ref_products
statement ok
ALTER TABLE orders ADD FOREIGN KEY (product) REFERENCES products ON UPDATE NO ACTION
statement ok
ALTER TABLE orders DROP CONSTRAINT fk_product_ref_products
statement ok
ALTER TABLE orders ADD FOREIGN KEY (product) REFERENCES products ON DELETE CASCADE
statement ok
ALTER TABLE orders DROP CONSTRAINT fk_product_ref_products
statement ok
ALTER TABLE orders ADD FOREIGN KEY (product) REFERENCES products ON UPDATE CASCADE
statement ok
ALTER TABLE orders DROP CONSTRAINT fk_product_ref_products
statement ok
ALTER TABLE orders ADD FOREIGN KEY (product) REFERENCES products ON DELETE SET NULL
statement ok
ALTER TABLE orders DROP CONSTRAINT fk_product_ref_products
statement ok
ALTER TABLE orders ADD FOREIGN KEY (product) REFERENCES products ON UPDATE SET NULL
statement ok
ALTER TABLE orders DROP CONSTRAINT fk_product_ref_products
statement ok
ALTER TABLE orders ADD FOREIGN KEY (product) REFERENCES products ON DELETE SET DEFAULT
statement ok
ALTER TABLE orders DROP CONSTRAINT fk_product_ref_products
statement ok
ALTER TABLE orders ADD FOREIGN KEY (product) REFERENCES products ON UPDATE SET DEFAULT
statement ok
ALTER TABLE orders DROP CONSTRAINT fk_product_ref_products
statement ok
ALTER TABLE orders ADD FOREIGN KEY (product) REFERENCES products ON DELETE RESTRICT ON UPDATE NO ACTION
statement ok
ALTER TABLE orders DROP CONSTRAINT fk_product_ref_products
statement ok
ALTER TABLE orders ADD FOREIGN KEY (product) REFERENCES products ON DELETE RESTRICT ON UPDATE RESTRICT
statement ok
ALTER TABLE orders VALIDATE CONSTRAINT fk_product_ref_products
statement ok
CREATE DATABASE "user content"
# "reviews" makes "products" have multiple inbound references, as well as making
# "orders" have both directions, and makes sure that we're handling escaping and
# cross-database references.
statement ok
CREATE TABLE "user content"."customer reviews" (
id INT PRIMARY KEY,
product STRING NOT NULL REFERENCES products,
customer INT,
"order" INT,
shipment int,
body STRING,
CONSTRAINT customerfk FOREIGN KEY (customer) REFERENCES customers,
CONSTRAINT orderfk FOREIGN KEY ("order", shipment) REFERENCES orders (id, shipment),
INDEX (product),
INDEX (customer),
INDEX ("order")
)
statement ok
INSERT INTO orders VALUES (1, 1, '780', 2)
statement error foreign key violation: value \['fake'\] not found in products@primary
SET tracing = on,kv,results; INSERT INTO orders VALUES (2, 2, 'fake', 2)
statement ok
SET tracing = off
query T rowsort
SELECT message FROM [SHOW KV TRACE FOR SESSION]
WHERE message LIKE 'FKScan%'
----
FKScan /Table/54/1/"fake"{-/#}
FKScan /Table/53/1/2{-/#}
statement error pgcode 23503 foreign key violation: values \['780'\] in columns \[sku\] referenced in table "orders"
DELETE FROM products
statement ok
INSERT INTO "user content"."customer reviews" VALUES (1, '780', 2, 1, 1, NULL)
statement error pgcode 23503 foreign key violation: value \['790'\] not found in products@primary \[sku\]
INSERT INTO "user content"."customer reviews" (id, product, body) VALUES (2, '790', 'would not buy again')
statement ok
INSERT INTO "user content"."customer reviews" (id, product, body) VALUES (2, '780', 'would not buy again')
statement ok
CREATE TABLE "user content".review_stats (
id INT PRIMARY KEY,
upvotes INT,
CONSTRAINT reviewfk FOREIGN KEY (id) REFERENCES "user content"."customer reviews"
)
query TTTTB
SHOW CONSTRAINTS FROM "user content".review_stats
----
review_stats primary PRIMARY KEY PRIMARY KEY (id ASC) true
review_stats reviewfk FOREIGN KEY FOREIGN KEY (id) REFERENCES "customer reviews" (id) true
statement error pgcode 23503 foreign key violation: value \[5\] not found in customer reviews@primary \[id\]
INSERT INTO "user content".review_stats (id, upvotes) VALUES (5, 1)
statement ok
INSERT INTO "user content".review_stats (id, upvotes) VALUES (2, 1)
statement error pgcode 23503 foreign key violation: values \[2\] in columns \[id\] referenced in table "review_stats"
DELETE FROM "user content"."customer reviews" WHERE id = 2
statement ok
ALTER TABLE "user content".review_stats DROP CONSTRAINT reviewfk
query TTTTB
SHOW CONSTRAINTS FROM "user content".review_stats
----
review_stats primary PRIMARY KEY PRIMARY KEY (id ASC) true
statement ok
DELETE FROM "user content"."customer reviews"
statement error pgcode 23503 foreign key violation: value \['790'\] not found in products@primary \[sku\]
INSERT INTO orders VALUES (2, 1, '790', 2)
statement error pgcode 23503 foreign key violation: value \[43\] not found in customers@primary \[id\]
INSERT INTO orders VALUES (2, 1, '780', 43)
statement ok
INSERT INTO orders VALUES (2, 1, '780', 1)
# Try to point to missing FK.
statement error pgcode 23503 foreign key violation: value \['790'\] not found in products@primary \[sku\]
UPDATE orders SET product = '790' WHERE id = 2
# Try to point to missing fk *while changing PK*.
statement error pgcode 23503 foreign key violation: value \['790'\] not found in products@primary \[sku\]
UPDATE orders SET id = 3, product = '790' WHERE id = 2
# Change PK while leaving everything else is fine though.
statement ok
UPDATE orders SET id = 3 WHERE id = 2
# Change PK and point to different product.
statement ok
UPDATE orders SET id = 2, product = 'VP-W9QH-W44L' WHERE id = 3
statement ok
UPDATE orders SET product = '780' WHERE id = 2
# "delivery" is interesting since it references a secondary index with different col names.
statement ok
CREATE TABLE delivery (
ts TIMESTAMP DEFAULT now(),
"order" int,
shipment int,
item STRING REFERENCES products (upc),
FOREIGN KEY ("order", shipment) REFERENCES orders (id, shipment),
INDEX (item)
)
query TT
SHOW CREATE TABLE delivery
----
delivery CREATE TABLE delivery (
ts TIMESTAMP NULL DEFAULT now():::TIMESTAMP,
"order" INT8 NULL,
shipment INT8 NULL,
item STRING NULL,
CONSTRAINT fk_item_ref_products FOREIGN KEY (item) REFERENCES products (upc),
INDEX delivery_item_idx (item ASC),
CONSTRAINT fk_order_ref_orders FOREIGN KEY ("order", shipment) REFERENCES orders (id, shipment),
INDEX delivery_auto_index_fk_order_ref_orders ("order" ASC, shipment ASC),
FAMILY "primary" (ts, "order", shipment, item, rowid)
)
statement ok
INSERT INTO delivery ("order", shipment, item) VALUES
(1, 1, '867072000006'), (1, 1, '867072000006'), (1, 1, '885155001450'), (1, 1, '867072000006')
statement error pgcode 23503 foreign key violation: value \['missing'\] not found in products@products_upc_key \[upc\]
INSERT INTO delivery ("order", shipment, item) VALUES
(1, 1, '867072000006'), (1, 1, 'missing'), (1, 1, '885155001450'), (1, 1, '867072000006')
statement error pgcode 23503 foreign key violation: value \[1 99\] not found in orders@primary \[id shipment\]
INSERT INTO delivery ("order", shipment, item) VALUES
(1, 1, '867072000006'), (1, 99, '867072000006')
statement error pgcode 23503 foreign key violation: values \['867072000006'\] in columns \[upc\] referenced in table "delivery"
DELETE FROM products WHERE sku = 'VP-W9QH-W44L'
# Blanking a field nobody cares about is fine.
statement ok
UPDATE products SET vendor = '' WHERE sku = '780'
# No-op update should be fine.
statement ok
UPDATE products SET sku = '770' WHERE sku = '750'
# Changing referenced PK fails.
statement error pgcode 23503 foreign key violation: values \['780'\] in columns \[sku\] referenced in table "orders"
UPDATE products SET sku = '770' WHERE sku = '780'
# No-op change to existing data is fine.
statement ok
UPDATE products SET upc = '885155001450' WHERE sku = '780'
# Changing referenced non-pk index fails.
statement error pgcode 23503 foreign key violation: values \['885155001450'\] in columns \[upc\] referenced in table "delivery"
UPDATE products SET upc = 'blah' WHERE sku = '780'
statement ok
ALTER TABLE delivery DROP CONSTRAINT fk_item_ref_products
statement ok
UPDATE products SET upc = 'blah' WHERE sku = '780'
statement ok
ALTER TABLE delivery ADD FOREIGN KEY (item) REFERENCES products (upc)
query TTTTB
SHOW CONSTRAINTS FROM delivery
----
delivery fk_item_ref_products FOREIGN KEY FOREIGN KEY (item) REFERENCES products (upc) false
delivery fk_order_ref_orders FOREIGN KEY FOREIGN KEY ("order", shipment) REFERENCES orders (id, shipment) true
statement error pgcode 23503 foreign key violation: "delivery" row item='885155001450' has no match in "products"
ALTER TABLE delivery VALIDATE CONSTRAINT fk_item_ref_products
query TTTTB
SHOW CONSTRAINTS FROM delivery
----
delivery fk_item_ref_products FOREIGN KEY FOREIGN KEY (item) REFERENCES products (upc) false
delivery fk_order_ref_orders FOREIGN KEY FOREIGN KEY ("order", shipment) REFERENCES orders (id, shipment) true
statement ok
UPDATE products SET upc = '885155001450' WHERE sku = '780'
# Changing referenced non-pk index fails once again with constraint re-added.
statement error pgcode 23503 foreign key violation: values \['885155001450'\] in columns \[upc\] referenced in table "delivery"
UPDATE products SET upc = 'blah' WHERE sku = '780'
statement ok
ALTER TABLE delivery VALIDATE CONSTRAINT fk_item_ref_products
query TTTTB
SHOW CONSTRAINTS FROM delivery
----
delivery fk_item_ref_products FOREIGN KEY FOREIGN KEY (item) REFERENCES products (upc) true
delivery fk_order_ref_orders FOREIGN KEY FOREIGN KEY ("order", shipment) REFERENCES orders (id, shipment) true
statement ok
ALTER TABLE "user content"."customer reviews"
DROP CONSTRAINT orderfk
statement ok
INSERT INTO "user content"."customer reviews" (id, product, body, "order") VALUES (3, '780', 'i ordered 100 of them', 9)
statement ok
ALTER TABLE "user content"."customer reviews"
ADD CONSTRAINT orderfk2 FOREIGN KEY ("order", shipment) REFERENCES orders (id, shipment)
# This is allowed because we match using MATCH SIMPLE.
statement ok
ALTER TABLE "user content"."customer reviews"
VALIDATE CONSTRAINT orderfk2
# This is allowed because we match using MATCH SIMPLE.
statement ok
INSERT INTO "user content"."customer reviews" (id, product, body, "order") VALUES (4, '780', 'i ordered 101 of them', 9)
statement error pgcode 23503 foreign key violation: value \[9 1\] not found in orders@primary \[id shipment\]
INSERT INTO "user content"."customer reviews" (id, product, body, "order", shipment) VALUES (4, '780', 'i ordered 101 of them', 9, 1)
statement error pgcode 23503 foreign key violation: value \[1 9\] not found in orders@primary \[id shipment\]
INSERT INTO "user content"."customer reviews" (id, product, body, shipment, "order") VALUES (4, '780', 'i ordered 101 of them', 9, 1)
statement ok
ALTER TABLE delivery DROP CONSTRAINT fk_order_ref_orders
statement ok
TRUNCATE orders, "user content"."customer reviews"
# Changing now non-referenced and secondary field is fine.
statement ok
UPDATE products SET sku = '750', vendor = 'roomba' WHERE sku = '780'
# Changing PK and referenced secondary index is not ok.
statement error pgcode 23503 foreign key violation: values \['885155001450'\] in columns \[upc\] referenced in table "delivery"
UPDATE products SET sku = '780', upc = 'blah' WHERE sku = '750'
statement error pgcode 23503 foreign key violation: values \['885155001450'\] in columns \[upc\] referenced in table "delivery"
DELETE FROM products WHERE sku = '750'
statement error "products" is referenced by foreign key from table "orders"
TRUNCATE products
query I
SELECT count(*) FROM delivery
----
4
statement ok
TRUNCATE products CASCADE
query I
SELECT count(*) FROM delivery
----
0
statement ok
TRUNCATE delivery, products, orders, "user content"."customer reviews"
query TTTTB colnames
SHOW CONSTRAINTS FROM orders
----
table_name constraint_name constraint_type details validated
orders fk_product_ref_products FOREIGN KEY FOREIGN KEY (product) REFERENCES products (sku) ON DELETE RESTRICT ON UPDATE RESTRICT true
orders primary PRIMARY KEY PRIMARY KEY (id ASC, shipment ASC) true
orders valid_customer FOREIGN KEY FOREIGN KEY (customer) REFERENCES customers (id) true
statement error "products_upc_key" is referenced by foreign key from table "delivery"
DROP INDEX products@products_upc_key
statement error "products_upc_key" is referenced by foreign key from table "delivery"
DROP INDEX products@products_upc_key RESTRICT
statement error "products_upc_key" is referenced by foreign key from table "delivery"
ALTER TABLE products DROP COLUMN upc
statement ok
ALTER TABLE delivery DROP COLUMN "item"
statement ok
DROP INDEX products@products_upc_key CASCADE
statement error index "orders_product_idx" is in use as a foreign key constraint
DROP INDEX orders@orders_product_idx
statement error index "orders_product_idx" is in use as a foreign key constraint
DROP INDEX orders@orders_product_idx RESTRICT
statement error "products" is referenced by foreign key from table "orders"
DROP TABLE products
statement error referenced by foreign key from table "orders"
DROP TABLE products RESTRICT
statement error referenced by foreign key from table "customer reviews"
DROP TABLE orders
# reviews has a multi-col FK in which dropping one col is not allowed.
statement error column "order" is referenced by existing index "customer reviews_auto_index_orderfk"
ALTER TABLE "user content"."customer reviews" DROP COLUMN "order"
statement ok
ALTER TABLE "user content"."customer reviews" DROP COLUMN "order" CASCADE
statement ok
DROP TABLE "user content"."customer reviews"
statement ok
DROP TABLE orders
statement ok
DROP TABLE products
statement ok
CREATE TABLE parent (id int primary key)
statement ok
CREATE TABLE child (id INT PRIMARY KEY, parent_id INT UNIQUE REFERENCES parent)
statement ok
CREATE TABLE grandchild (id INT PRIMARY KEY, parent_id INT REFERENCES child (parent_id), INDEX (parent_id))
statement error "parent" is referenced by foreign key from table "child"
DROP TABLE parent
statement error "child" is referenced by foreign key from table "grandchild"
DROP TABLE child
statement error pgcode 23503 foreign key violation
INSERT INTO child VALUES (2, 2)
statement ok
DROP TABLE parent CASCADE
statement ok
INSERT INTO child VALUES (2, 2)
statement error pgcode 23503 foreign key violation
INSERT INTO grandchild VALUES (1, 1)
statement error in use as a foreign key constraint
DROP INDEX grandchild@grandchild_parent_id_idx
statement ok
DROP INDEX grandchild@grandchild_parent_id_idx CASCADE
statement ok
INSERT INTO grandchild VALUES (1, 1)
statement ok
DROP TABLE grandchild
statement ok
CREATE TABLE grandchild (id INT PRIMARY KEY, parent_id INT REFERENCES child (parent_id), INDEX (parent_id))
statement error pgcode 23503 foreign key violation
INSERT INTO grandchild VALUES (1, 1)
statement error "child_parent_id_key" is referenced by foreign key from table "grandchild"
DROP INDEX child@child_parent_id_key
statement ok
DROP INDEX child@child_parent_id_key CASCADE
statement ok
INSERT INTO grandchild VALUES (1, 1)
statement ok
CREATE TABLE employees (id INT PRIMARY KEY, manager INT REFERENCES employees, INDEX (manager))
statement ok
INSERT INTO employees VALUES (1, NULL)
statement ok
INSERT INTO employees VALUES (2, 1), (3, 1)
statement ok
INSERT INTO employees VALUES (4, 2), (5, 3)
statement error pgcode 23503 foreign key violation
DELETE FROM employees WHERE id = 2
statement error pgcode 23503 foreign key violation
DELETE FROM employees WHERE id > 1
statement ok
DROP TABLE employees
statement ok
CREATE TABLE pairs (id INT PRIMARY KEY, src INT, dest STRING, UNIQUE (src, dest))
statement ok
INSERT INTO pairs VALUES (1, 100, 'one'), (2, 200, 'two')
statement error type of "b" \(STRING\) does not match foreign key "pairs"."id" \(INT\)
CREATE TABLE refpairs (a INT, b STRING, CONSTRAINT fk FOREIGN KEY (b) REFERENCES pairs)
statement error 2 columns must reference exactly 2 columns in referenced table \(found 1\)
CREATE TABLE refpairs (a INT, b STRING, CONSTRAINT fk FOREIGN KEY (a, b) REFERENCES pairs)
# TODO(dt): remove ordering constraint on matching index
statement ok
CREATE TABLE refpairs_wrong_order (
a INT,
b STRING,
FOREIGN KEY (a, b) REFERENCES pairs (src, dest),
INDEX (b, a)
)
query TTBITTBB colnames
SHOW INDEXES FROM refpairs_wrong_order
----
table_name index_name non_unique seq_in_index column_name direction storing implicit
refpairs_wrong_order primary false 1 rowid ASC false false
refpairs_wrong_order refpairs_wrong_order_b_a_idx true 1 b ASC false false
refpairs_wrong_order refpairs_wrong_order_b_a_idx true 2 a ASC false false
refpairs_wrong_order refpairs_wrong_order_b_a_idx true 3 rowid ASC false true
refpairs_wrong_order refpairs_wrong_order_auto_index_fk_a_ref_pairs true 1 a ASC false false
refpairs_wrong_order refpairs_wrong_order_auto_index_fk_a_ref_pairs true 2 b ASC false false
refpairs_wrong_order refpairs_wrong_order_auto_index_fk_a_ref_pairs true 3 rowid ASC false true
statement ok
CREATE TABLE refpairs_c_between (a INT, b STRING, c INT, FOREIGN KEY (a, b) REFERENCES pairs (src, dest), INDEX (a, c, b))
query TTBITTBB colnames
SHOW INDEXES FROM refpairs_c_between
----
table_name index_name non_unique seq_in_index column_name direction storing implicit
refpairs_c_between primary false 1 rowid ASC false false
refpairs_c_between refpairs_c_between_a_c_b_idx true 1 a ASC false false
refpairs_c_between refpairs_c_between_a_c_b_idx true 2 c ASC false false
refpairs_c_between refpairs_c_between_a_c_b_idx true 3 b ASC false false
refpairs_c_between refpairs_c_between_a_c_b_idx true 4 rowid ASC false true
refpairs_c_between refpairs_c_between_auto_index_fk_a_ref_pairs true 1 a ASC false false
refpairs_c_between refpairs_c_between_auto_index_fk_a_ref_pairs true 2 b ASC false false
refpairs_c_between refpairs_c_between_auto_index_fk_a_ref_pairs true 3 rowid ASC false true
statement ok
CREATE TABLE refpairs (
a INT,
b STRING,
c INT,
FOREIGN KEY (a, b) REFERENCES pairs (src, dest) ON UPDATE RESTRICT,
INDEX (a, b, c)
)
query TTBITTBB colnames
SHOW INDEXES FROM refpairs
----
table_name index_name non_unique seq_in_index column_name direction storing implicit
refpairs primary false 1 rowid ASC false false
refpairs refpairs_a_b_c_idx true 1 a ASC false false
refpairs refpairs_a_b_c_idx true 2 b ASC false false
refpairs refpairs_a_b_c_idx true 3 c ASC false false
refpairs refpairs_a_b_c_idx true 4 rowid ASC false true
query TT
SHOW CREATE TABLE refpairs
----
refpairs CREATE TABLE refpairs (
a INT8 NULL,
b STRING NULL,
c INT8 NULL,
CONSTRAINT fk_a_ref_pairs FOREIGN KEY (a, b) REFERENCES pairs (src, dest) ON UPDATE RESTRICT,
INDEX refpairs_a_b_c_idx (a ASC, b ASC, c ASC),
FAMILY "primary" (a, b, c, rowid)
)
statement error pgcode 23503 foreign key violation: value \[100 'two'\] not found in pairs@pairs_src_dest_key \[src dest\]
INSERT INTO refpairs VALUES (100, 'two'), (200, 'two')
statement ok
INSERT INTO refpairs VALUES (100, 'one', 3), (200, 'two', null)
statement error pgcode 23503 foreign key violation: values \[200 'two'\] in columns \[src dest\] referenced in table "refpairs"
UPDATE pairs SET dest = 'too' WHERE id = 2
statement error pgcode 23503 foreign key violation: values \[200 'two'\] in columns \[src dest\] referenced in table "refpairs"
DELETE FROM pairs WHERE id = 2
statement error pgcode 23503 foreign key violation: values \[100 'one'\] in columns \[src dest\] referenced in table "refpairs"
DELETE FROM pairs WHERE id = 1
statement error foreign key violation: values \[100 'one'\] in columns \[src dest\] referenced in table "refpairs"
SET tracing = on,kv; DELETE FROM pairs WHERE id = 1
statement ok
SET tracing=off
# Test that fk scans on indexes that are longer than the foreign key use
# PrefixEnd instead of interleave end.
query T rowsort
SELECT message FROM [SHOW KV TRACE FOR SESSION]
WHERE message LIKE 'FKScan%'
----
FKScan /Table/85/3/100/"one"{-/#}
FKScan /Table/86/3/100/"one"{-/#}
FKScan /Table/87/2/100/"one"{-/PrefixEnd}
# since PKs are handled differently than other indexes, check pk<->pk ref with no other indexes in play.
statement ok
CREATE TABLE foo (id INT PRIMARY KEY)
statement ok
CREATE TABLE bar (id INT PRIMARY KEY REFERENCES foo)
statement ok
INSERT INTO foo VALUES (2)
statement ok
INSERT INTO bar VALUES (2)
statement error pgcode 23503 foreign key violation: values \[2] in columns \[id\] referenced in table "bar"
DELETE FROM foo
statement ok
CREATE DATABASE otherdb
statement ok
CREATE TABLE otherdb.othertable (id INT PRIMARY KEY)
statement ok
CREATE TABLE crossdb (id INT PRIMARY KEY, FOREIGN KEY (id) REFERENCES otherdb.othertable)
statement error pgcode 23503 foreign key violation: value \[2\] not found in othertable@primary \[id\]
INSERT INTO crossdb VALUES (2)
statement ok
INSERT INTO otherdb.othertable VALUES (1), (2)
statement ok
INSERT INTO crossdb VALUES (2)
statement error pgcode 23503 foreign key violation: values \[2] in columns \[id\] referenced in table "crossdb"
DELETE FROM otherdb.othertable WHERE id = 2
statement error "othertable" is referenced by foreign key from table "crossdb"
DROP TABLE otherdb.othertable
statement ok
DROP TABLE otherdb.othertable, crossdb
statement ok
CREATE TABLE modules (id BIGSERIAL NOT NULL PRIMARY KEY)
statement ok
CREATE TABLE domains (id BIGSERIAL NOT NULL PRIMARY KEY)
# We'll use the unique index for the domain fk (since it is a prefix), but we
# we correctly only mark the prefix as used and thus still allow module_id to be
# used in another FK.
statement ok
CREATE TABLE domain_modules (
id BIGSERIAL NOT NULL PRIMARY KEY,
domain_id BIGINT NOT NULL,
module_id BIGINT NOT NULL,
CONSTRAINT domain_modules_domain_id_fk FOREIGN KEY (domain_id) REFERENCES domains (id),
CONSTRAINT domain_modules_module_id_fk FOREIGN KEY (module_id) REFERENCES modules (id),
CONSTRAINT domain_modules_uq UNIQUE (domain_id, module_id)
)
query TTTTB
SHOW CONSTRAINTS FROM domain_modules
----
domain_modules domain_modules_domain_id_fk FOREIGN KEY FOREIGN KEY (domain_id) REFERENCES domains (id) true
domain_modules domain_modules_module_id_fk FOREIGN KEY FOREIGN KEY (module_id) REFERENCES modules (id) true
domain_modules domain_modules_uq UNIQUE UNIQUE (domain_id ASC, module_id ASC) true
domain_modules primary PRIMARY KEY PRIMARY KEY (id ASC) true
statement ok
INSERT INTO modules VALUES(3)
statement error foreign key violation: value \[2\] not found in domains@primary
SET tracing = on,kv; INSERT INTO domain_modules VALUES (1, 2, 3)
statement ok
SET tracing=off
query T rowsort
SELECT message FROM [SHOW KV TRACE FOR SESSION]
WHERE message LIKE 'FKScan%'
----
FKScan /Table/93/1/3{-/#}
FKScan /Table/94/1/2{-/#}
statement ok
CREATE TABLE tx (
id INT NOT NULL PRIMARY KEY
)
statement ok
CREATE TABLE tx_leg (
leg_id SERIAL NOT NULL PRIMARY KEY,
tx_id INT NOT NULL REFERENCES tx
)
statement ok
BEGIN TRANSACTION
statement ok
INSERT INTO tx VALUES (2)
statement ok
INSERT INTO tx_leg VALUES (201, 2);
statement ok
INSERT INTO tx_leg VALUES (202, 2);
statement ok
COMMIT
statement ok
BEGIN TRANSACTION
statement error pgcode 23503 foreign key violation: value \[3\] not found in tx@primary \[id\]
INSERT INTO tx_leg VALUES (302, 3);
statement ok
COMMIT
statement ok
CREATE TABLE a (id SERIAL NOT NULL, self_id INT, b_id INT NOT NULL, PRIMARY KEY (id))
statement ok
CREATE TABLE b (id SERIAL NOT NULL, PRIMARY KEY (id))
# The index needed for the fk constraint is automatically added because the table is empty
statement ok
ALTER TABLE a ADD CONSTRAINT fk_self_id FOREIGN KEY (self_id) REFERENCES a;
# The index needed for the fk constraint is automatically added because the table is empty
statement ok
ALTER TABLE a ADD CONSTRAINT fk_b FOREIGN KEY (b_id) REFERENCES b;
statement ok
INSERT INTO b VALUES (1), (2), (3);
statement ok
INSERT INTO a VALUES (1, NULL, 1)
statement ok
INSERT INTO a VALUES (2, 1, 1), (3, 1, 2)
statement ok
INSERT INTO a VALUES (4, 2, 2)
statement ok
DELETE FROM b WHERE id = 3
statement error pgcode 23503 foreign key violation
DELETE FROM b WHERE id = 2
statement error pgcode 23503 foreign key violation
DELETE FROM a WHERE id = 1
statement ok
DELETE FROM a WHERE id > 2
statement ok
DELETE FROM b WHERE id = 2
statement ok
DROP TABLE a
statement ok
DROP TABLE b
# A CREATE TABLE with a FK reference within a transaction.
statement ok
CREATE TABLE referee (id INT PRIMARY KEY);
statement ok
BEGIN TRANSACTION
statement ok
CREATE TABLE refers (
a INT REFERENCES referee,
b INT,
INDEX b_idx (b)
)
# Add some schema changes within the same transaction to verify that a
# table that isn't yet public can be modified.
statement ok
CREATE INDEX foo ON refers (a)
statement ok
ALTER INDEX refers@b_idx RENAME TO another_idx
query TT
SHOW CREATE TABLE refers
----
refers CREATE TABLE refers (
a INT8 NULL,
b INT8 NULL,
INDEX another_idx (b ASC),
CONSTRAINT fk_a_ref_referee FOREIGN KEY (a) REFERENCES referee (id),
INDEX refers_auto_index_fk_a_ref_referee (a ASC),
INDEX foo (a ASC),
FAMILY "primary" (a, b, rowid)
)
statement ok
DROP INDEX refers@another_idx
# refers is not visible because it is in the ADD state.
query T
SHOW TABLES FROM test
----
bar
child
customers
delivery
domain_modules
domains
foo
grandchild
modules
pairs
referee
refpairs
refpairs_c_between
refpairs_wrong_order
tx
tx_leg
unindexed
statement ok
COMMIT
# CREATE AND DROP a table with a fk in the same transaction.
statement ok
BEGIN TRANSACTION
statement ok
CREATE TABLE refers1 (a INT REFERENCES referee);
statement ok
DROP TABLE refers1
statement ok
COMMIT
# Check that removing self-ref FK correctly removed backref too, #16070.
statement ok
CREATE TABLE employee (
id INT PRIMARY KEY,
manager INT,
UNIQUE (manager)
);
statement ok
ALTER TABLE employee
ADD CONSTRAINT emp_emp
FOREIGN KEY (manager)
REFERENCES employee;
statement ok
ALTER TABLE employee
DROP CONSTRAINT emp_emp;
statement ok
SHOW CREATE TABLE employee;
# Ensure that tables with an fk reference from their pk appear correctly in
# SHOW CREATE TABLE (#17596).
statement ok
CREATE TABLE pkref_a (a INT PRIMARY KEY)
statement ok
CREATE TABLE pkref_b (b INT PRIMARY KEY REFERENCES pkref_a ON UPDATE NO ACTION ON DELETE RESTRICT)
query TT
SHOW CREATE TABLE pkref_b
----
pkref_b CREATE TABLE pkref_b (
b INT8 NOT NULL,
CONSTRAINT "primary" PRIMARY KEY (b ASC),
CONSTRAINT fk_b_ref_pkref_a FOREIGN KEY (b) REFERENCES pkref_a (a) ON DELETE RESTRICT,
FAMILY "primary" (b)
)
subtest 20042
statement ok
CREATE TABLE test20042 (
x STRING PRIMARY KEY
,y STRING UNIQUE
,z STRING REFERENCES test20042(y)
);
statement ok
INSERT INTO test20042 (x, y, z) VALUES ('pk1', 'k1', null);
statement ok
INSERT INTO test20042 (x, y, z) VALUES ('pk2', 'k2 ', 'k1');
statement ok
DELETE FROM test20042 WHERE x = 'pk2';
statement ok
DELETE FROM test20042 WHERE x = 'pk1';
subtest 20045
statement ok
CREATE TABLE test20045 (
x STRING PRIMARY KEY
,y STRING UNIQUE REFERENCES test20045(x)
,z STRING REFERENCES test20045(y)
);
statement ok
INSERT INTO test20045 (x, y, z) VALUES ('pk1', NULL, NULL);
statement ok
INSERT INTO test20045 (x, y, z) VALUES ('pk2', 'pk1', NULL);
statement ok
INSERT INTO test20045 (x, y, z) VALUES ('pk3', 'pk2', 'pk1');
statement ok
DELETE FROM test20045 WHERE x = 'pk3';
statement ok
DELETE FROM test20045 WHERE x = 'pk2';
statement ok
DELETE FROM test20045 WHERE x = 'pk1';
## Delete cascade without privileges
statement ok
CREATE DATABASE d;
statement ok
CREATE TABLE d.a (
id STRING PRIMARY KEY
);
statement ok
CREATE TABLE d.b (
id STRING PRIMARY KEY
,a_id STRING REFERENCES d.a ON DELETE CASCADE
);
statement ok
INSERT INTO d.a VALUES ('a1');
statement ok
INSERT INTO d.b VALUES ('b1', 'a1');
statement ok
GRANT ALL ON DATABASE d TO testuser;
statement ok
GRANT ALL ON d.a TO testuser;
user testuser