Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
70658: sql: match postgres FOREIGN KEY constraint name r=ajwerner a=otan

See individual commits for details.

Co-authored-by: Oliver Tan <[email protected]>
  • Loading branch information
craig[bot] and otan committed Oct 14, 2021
2 parents d38cff4 + 7a2796a commit 44226ec
Show file tree
Hide file tree
Showing 46 changed files with 404 additions and 393 deletions.
22 changes: 11 additions & 11 deletions pkg/ccl/backupccl/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3268,25 +3268,25 @@ func TestBackupRestoreCrossTableReferences(t *testing.T) {

// FK validation on customers from receipts is preserved.
db.ExpectErr(
t, "update.*violates foreign key constraint \"fk_dest_ref_customers\"",
t, "update.*violates foreign key constraint \"receipts_dest_fkey\"",
`UPDATE store.customers SET email = concat(id::string, 'nope')`,
)

// FK validation on customers from orders is preserved.
db.ExpectErr(
t, "update.*violates foreign key constraint \"fk_customerid_ref_customers\"",
t, "update.*violates foreign key constraint \"orders_customerid_fkey\"",
`UPDATE store.customers SET id = id * 1000`,
)

// FK validation of customer id is preserved.
db.ExpectErr(
t, "insert.*violates foreign key constraint \"fk_customerid_ref_customers\"",
t, "insert.*violates foreign key constraint \"orders_customerid_fkey\"",
`INSERT INTO store.orders VALUES (999, NULL, 999)`,
)

// FK validation of self-FK is preserved.
db.ExpectErr(
t, "insert.*violates foreign key constraint \"fk_reissue_ref_receipts\"",
t, "insert.*violates foreign key constraint \"receipts_reissue_fkey\"",
`INSERT INTO store.receipts VALUES (1, 999, NULL, NULL)`,
)
})
Expand All @@ -3301,7 +3301,7 @@ func TestBackupRestoreCrossTableReferences(t *testing.T) {

// FK validation on customers from orders is preserved.
db.ExpectErr(
t, "update.*violates foreign key constraint \"fk_customerid_ref_customers\"",
t, "update.*violates foreign key constraint \"orders_customerid_fkey\"",
`UPDATE store.customers SET id = id*100`,
)

Expand Down Expand Up @@ -3342,7 +3342,7 @@ func TestBackupRestoreCrossTableReferences(t *testing.T) {

// FK validation of self-FK is preserved.
db.ExpectErr(
t, "insert.*violates foreign key constraint \"fk_reissue_ref_receipts\"",
t, "insert.*violates foreign key constraint \"receipts_reissue_fkey\"",
`INSERT INTO store.receipts VALUES (-1, 999, NULL, NULL)`,
)
})
Expand All @@ -3360,19 +3360,19 @@ func TestBackupRestoreCrossTableReferences(t *testing.T) {

// FK validation of customer email is preserved.
db.ExpectErr(
t, "nsert.*violates foreign key constraint \"fk_dest_ref_customers\"",
t, "nsert.*violates foreign key constraint \"receipts_dest_fkey\"",
`INSERT INTO store.receipts VALUES (-1, NULL, '999', 999)`,
)

// FK validation on customers from receipts is preserved.
db.ExpectErr(
t, "delete.*violates foreign key constraint \"fk_dest_ref_customers\"",
t, "delete.*violates foreign key constraint \"receipts_dest_fkey\"",
`DELETE FROM store.customers`,
)

// FK validation of self-FK is preserved.
db.ExpectErr(
t, "insert.*violates foreign key constraint \"fk_reissue_ref_receipts\"",
t, "insert.*violates foreign key constraint \"receipts_reissue_fkey\"",
`INSERT INTO store.receipts VALUES (-1, 999, NULL, NULL)`,
)
})
Expand Down Expand Up @@ -3429,7 +3429,7 @@ func TestBackupRestoreCrossTableReferences(t *testing.T) {
db.Exec(t, `RESTORE store.customers, storestats.ordercounts, store.orders FROM $1`, LocalFoo)

// we want to observe just the view-related errors, not fk errors below.
db.Exec(t, `ALTER TABLE store.orders DROP CONSTRAINT fk_customerid_ref_customers`)
db.Exec(t, `ALTER TABLE store.orders DROP CONSTRAINT orders_customerid_fkey`)

// customers is aware of the view that depends on it.
db.ExpectErr(
Expand All @@ -3452,7 +3452,7 @@ func TestBackupRestoreCrossTableReferences(t *testing.T) {
db.Exec(t, `CREATE DATABASE otherstore`)
db.Exec(t, `RESTORE store.* FROM $1 WITH into_db = 'otherstore'`, LocalFoo)
// we want to observe just the view-related errors, not fk errors below.
db.Exec(t, `ALTER TABLE otherstore.orders DROP CONSTRAINT fk_customerid_ref_customers`)
db.Exec(t, `ALTER TABLE otherstore.orders DROP CONSTRAINT orders_customerid_fkey`)
db.Exec(t, `DROP TABLE otherstore.receipts`)

db.ExpectErr(
Expand Down
4 changes: 2 additions & 2 deletions pkg/ccl/backupccl/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,14 @@ ORDER BY object_type, object_name`, full)
a INT8 NOT NULL,
b INT8 NULL,
CONSTRAINT fkreftable_pkey PRIMARY KEY (a ASC),
CONSTRAINT fk_b_ref_fksrc FOREIGN KEY (b) REFERENCES public.fksrc(a),
CONSTRAINT fkreftable_b_fkey FOREIGN KEY (b) REFERENCES public.fksrc(a),
FAMILY "primary" (a, b)
)`
wantDiffDB := `CREATE TABLE fkreftable (
a INT8 NOT NULL,
b INT8 NULL,
CONSTRAINT fkreftable_pkey PRIMARY KEY (a ASC),
CONSTRAINT fk_b_ref_fksrc FOREIGN KEY (b) REFERENCES data.public.fksrc(a),
CONSTRAINT fkreftable_b_fkey FOREIGN KEY (b) REFERENCES data.public.fksrc(a),
FAMILY "primary" (a, b)
)`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ CREATE TABLE public.fk_using_implicit_columns_against_t_one_family (
ref_t_pk INT8 NOT NULL,
ref_t_c INT8 NOT NULL,
CONSTRAINT fk_using_implicit_columns_against_t_one_family_pkey PRIMARY KEY (pk ASC),
CONSTRAINT fk_ref_t_pk_ref_t FOREIGN KEY (ref_t_pk) REFERENCES public.t(pk),
CONSTRAINT fk_ref_t_c_ref_t FOREIGN KEY (ref_t_c) REFERENCES public.t(c),
CONSTRAINT fk_using_implicit_columns_against_t_one_family_ref_t_pk_fkey FOREIGN KEY (ref_t_pk) REFERENCES public.t(pk),
CONSTRAINT fk_using_implicit_columns_against_t_one_family_ref_t_c_fkey FOREIGN KEY (ref_t_c) REFERENCES public.t(c),
FAMILY fam_0_pk_ref_t_pk_ref_t_c (pk, ref_t_pk, ref_t_c)
)

Expand Down
4 changes: 2 additions & 2 deletions pkg/ccl/logictestccl/testdata/logic_test/regional_by_row
Original file line number Diff line number Diff line change
Expand Up @@ -1431,8 +1431,8 @@ regional_by_row_fk CREATE TABLE public.regional_by_row_fk (
crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region,
CONSTRAINT regional_by_row_fk_pkey PRIMARY KEY (f ASC),
CONSTRAINT ij_fk FOREIGN KEY (i, j) REFERENCES public.regional_by_row_unique_in_column(e, d),
CONSTRAINT fk_g_ref_regional_by_row_unique_in_column FOREIGN KEY (g) REFERENCES public.regional_by_row_unique_in_column(a),
CONSTRAINT fk_h_ref_regional_by_row_unique_in_column FOREIGN KEY (h) REFERENCES public.regional_by_row_unique_in_column(b),
CONSTRAINT regional_by_row_fk_g_fkey FOREIGN KEY (g) REFERENCES public.regional_by_row_unique_in_column(a),
CONSTRAINT regional_by_row_fk_h_fkey FOREIGN KEY (h) REFERENCES public.regional_by_row_unique_in_column(b),
UNIQUE INDEX regional_by_row_fk_g_key (g ASC),
UNIQUE INDEX regional_by_row_fk_h_key (h ASC),
FAMILY fam_0_f_g_h_i_j_crdb_region (f, g, h, i, j, crdb_region)
Expand Down
8 changes: 8 additions & 0 deletions pkg/sql/catalog/tabledesc/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
package tabledesc

import (
"fmt"
"strings"

"github.com/cockroachdb/cockroach/pkg/geo/geoindex"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
Expand Down Expand Up @@ -523,3 +526,8 @@ func lazyAllocAppendIndex(slice *[]catalog.Index, idx catalog.Index, cap int) {
}
*slice = append(*slice, idx)
}

// ForeignKeyConstraintName forms a default foreign key constraint name.
func ForeignKeyConstraintName(fromTable string, columnNames []string) string {
return fmt.Sprintf("%s_%s_fkey", fromTable, strings.Join(columnNames, "_"))
}
8 changes: 4 additions & 4 deletions pkg/sql/comment_on_constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ func TestCommentOnConstraint(t *testing.T) {
gosql.NullString{String: `primary_comment`, Valid: true},
},
{
`COMMENT ON CONSTRAINT fk_b_ref_t ON t2 IS 'fk_comment'`,
`SELECT obj_description(oid, 'pg_constraint') FROM pg_constraint WHERE conname='fk_b_ref_t'`,
`COMMENT ON CONSTRAINT t2_b_fkey ON t2 IS 'fk_comment'`,
`SELECT obj_description(oid, 'pg_constraint') FROM pg_constraint WHERE conname='t2_b_fkey'`,
gosql.NullString{String: `fk_comment`, Valid: true},
},
{
`COMMENT ON CONSTRAINT fk_b_ref_t ON t2 IS 'fk_comment'; COMMENT ON CONSTRAINT fk_b_ref_t ON t2 IS NULL`,
`SELECT obj_description(oid, 'pg_constraint') FROM pg_constraint WHERE conname='fk_b_ref_t'`,
`COMMENT ON CONSTRAINT t2_b_fkey ON t2 IS 'fk_comment'; COMMENT ON CONSTRAINT t2_b_fkey ON t2 IS NULL`,
`SELECT obj_description(oid, 'pg_constraint') FROM pg_constraint WHERE conname='t2_b_fkey'`,
gosql.NullString{Valid: false},
},
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ func ResolveFK(
constraintName := string(d.Name)
if constraintName == "" {
constraintName = tabledesc.GenerateUniqueName(
fmt.Sprintf("fk_%s_ref_%s", string(d.FromCols[0]), target.Name),
tabledesc.ForeignKeyConstraintName(tbl.GetName(), d.FromCols.ToStrings()),
func(p string) bool {
_, ok := constraintInfo[p]
return ok
Expand Down
62 changes: 31 additions & 31 deletions pkg/sql/logictest/testdata/logic_test/alter_table
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ SHOW CONSTRAINTS FROM t
----
table_name constraint_name constraint_type details validated
t check_a CHECK CHECK ((a > 0)) true
t fk_f_ref_other FOREIGN KEY FOREIGN KEY (f) REFERENCES other(b) true
t foo UNIQUE UNIQUE (b ASC) true
t t_f_fkey FOREIGN KEY FOREIGN KEY (f) REFERENCES other(b) true
t t_pkey PRIMARY KEY PRIMARY KEY (a ASC) true

statement error CHECK
Expand All @@ -124,16 +124,16 @@ INSERT INTO t (a) VALUES (-3)
query TTTTB
SHOW CONSTRAINTS FROM t
----
t check_a CHECK CHECK ((a > 0)) true
t fk_f_ref_other FOREIGN KEY FOREIGN KEY (f) REFERENCES other(b) true
t foo UNIQUE UNIQUE (b ASC) true
t t_pkey PRIMARY KEY PRIMARY KEY (a ASC) true
t check_a CHECK CHECK ((a > 0)) true
t foo UNIQUE UNIQUE (b ASC) true
t t_f_fkey FOREIGN KEY FOREIGN KEY (f) REFERENCES other(b) true
t t_pkey PRIMARY KEY PRIMARY KEY (a ASC) true

statement error duplicate constraint name
ALTER TABLE t ADD CONSTRAINT check_a CHECK (a > 0)

statement error duplicate constraint name
ALTER TABLE t ADD CONSTRAINT fk_f_ref_other FOREIGN KEY (a) REFERENCES other (b)
ALTER TABLE t ADD CONSTRAINT t_f_fkey FOREIGN KEY (a) REFERENCES other (b)

# added constraints with generated names avoid name collisions.
statement ok
Expand All @@ -142,11 +142,11 @@ ALTER TABLE t ADD CHECK (a > 0)
query TTTTB
SHOW CONSTRAINTS FROM t
----
t check_a CHECK CHECK ((a > 0)) true
t check_a1 CHECK CHECK ((a > 0)) true
t fk_f_ref_other FOREIGN KEY FOREIGN KEY (f) REFERENCES other(b) true
t foo UNIQUE UNIQUE (b ASC) true
t t_pkey PRIMARY KEY PRIMARY KEY (a ASC) true
t check_a CHECK CHECK ((a > 0)) true
t check_a1 CHECK CHECK ((a > 0)) true
t foo UNIQUE UNIQUE (b ASC) true
t t_f_fkey FOREIGN KEY FOREIGN KEY (f) REFERENCES other(b) true
t t_pkey PRIMARY KEY PRIMARY KEY (a ASC) true

statement error constraint "typo" of relation "t" does not exist
ALTER TABLE t VALIDATE CONSTRAINT typo
Expand All @@ -164,11 +164,11 @@ ALTER TABLE t VALIDATE CONSTRAINT check_a
query TTTTB
SHOW CONSTRAINTS FROM t
----
t check_a CHECK CHECK ((a > 0)) true
t check_a1 CHECK CHECK ((a > 0)) true
t fk_f_ref_other FOREIGN KEY FOREIGN KEY (f) REFERENCES other(b) true
t foo UNIQUE UNIQUE (b ASC) true
t t_pkey PRIMARY KEY PRIMARY KEY (a ASC) true
t check_a CHECK CHECK ((a > 0)) true
t check_a1 CHECK CHECK ((a > 0)) true
t foo UNIQUE UNIQUE (b ASC) true
t t_f_fkey FOREIGN KEY FOREIGN KEY (f) REFERENCES other(b) true
t t_pkey PRIMARY KEY PRIMARY KEY (a ASC) true

statement ok
ALTER TABLE t DROP CONSTRAINT check_a, DROP CONSTRAINT check_a1
Expand Down Expand Up @@ -268,7 +268,7 @@ statement error foreign key
INSERT INTO t VALUES (31, 7, 32)

statement ok
ALTER TABLE t DROP CONSTRAINT fk_f_ref_other
ALTER TABLE t DROP CONSTRAINT t_f_fkey

statement ok
INSERT INTO t VALUES (31, 7, 32)
Expand Down Expand Up @@ -1122,7 +1122,7 @@ ALTER TABLE t2 ADD COLUMN x INT REFERENCES t1 (x)
statement ok
INSERT INTO t1 VALUES (1)

statement error pq: insert on table "t2" violates foreign key constraint "fk_x_ref_t1"
statement error pq: insert on table "t2" violates foreign key constraint "t2_x_fkey"
INSERT INTO t2 VALUES (2, 2)

statement ok
Expand All @@ -1137,7 +1137,7 @@ t2 CREATE TABLE public.t2 (
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
x INT8 NULL,
CONSTRAINT t2_pkey PRIMARY KEY (rowid ASC),
CONSTRAINT fk_x_ref_t1 FOREIGN KEY (x) REFERENCES public.t1(x),
CONSTRAINT t2_x_fkey FOREIGN KEY (x) REFERENCES public.t1(x),
FAMILY "primary" (y, rowid, x)
)

Expand All @@ -1157,7 +1157,7 @@ t3 CREATE TABLE public.t3 (
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
x INT8 NULL,
CONSTRAINT t3_pkey PRIMARY KEY (rowid ASC),
CONSTRAINT fk_x_ref_t1 FOREIGN KEY (x) REFERENCES public.t1(x),
CONSTRAINT t3_x_fkey FOREIGN KEY (x) REFERENCES public.t1(x),
UNIQUE INDEX t3_x_key (x ASC),
FAMILY "primary" (y, rowid, x)
)
Expand Down Expand Up @@ -1194,11 +1194,11 @@ t1 CREATE TABLE public.t1 (
x INT8 NOT NULL,
x2 INT8 NULL,
CONSTRAINT t1_pkey PRIMARY KEY (x ASC),
CONSTRAINT fk_x2_ref_t1 FOREIGN KEY (x2) REFERENCES public.t1(x),
CONSTRAINT t1_x2_fkey FOREIGN KEY (x2) REFERENCES public.t1(x),
FAMILY "primary" (x, x2)
)

statement error pq: insert on table "t1" violates foreign key constraint "fk_x2_ref_t1"
statement error pq: insert on table "t1" violates foreign key constraint "t1_x2_fkey"
INSERT INTO t1 VALUES (1, 2)

# Test ADD COL REFERENCES on a new table in the same txn.
Expand All @@ -1220,7 +1220,7 @@ t2 CREATE TABLE public.t2 (
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
x INT8 NULL,
CONSTRAINT t2_pkey PRIMARY KEY (rowid ASC),
CONSTRAINT fk_x_ref_t1 FOREIGN KEY (x) REFERENCES public.t1(x) NOT VALID,
CONSTRAINT t2_x_fkey FOREIGN KEY (x) REFERENCES public.t1(x) NOT VALID,
FAMILY "primary" (y, rowid, x)
)

Expand All @@ -1244,7 +1244,7 @@ t2 CREATE TABLE public.t2 (
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
x INT8 NULL,
CONSTRAINT t2_pkey PRIMARY KEY (rowid ASC),
CONSTRAINT fk_x_ref_t1 FOREIGN KEY (x) REFERENCES public.t1(x) NOT VALID,
CONSTRAINT t2_x_fkey FOREIGN KEY (x) REFERENCES public.t1(x) NOT VALID,
FAMILY "primary" (y, rowid, x)
)

Expand All @@ -1271,7 +1271,7 @@ t2 CREATE TABLE public.t2 (
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
x INT8 NULL,
CONSTRAINT t2_pkey PRIMARY KEY (rowid ASC),
CONSTRAINT fk_x_ref_t1 FOREIGN KEY (x) REFERENCES public.t1(x) NOT VALID,
CONSTRAINT t2_x_fkey FOREIGN KEY (x) REFERENCES public.t1(x) NOT VALID,
INDEX t2_x_idx (x ASC),
FAMILY "primary" (y, rowid, x)
)
Expand All @@ -1297,7 +1297,7 @@ t2 CREATE TABLE public.t2 (
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
x INT8 NULL,
CONSTRAINT t2_pkey PRIMARY KEY (rowid ASC),
CONSTRAINT fk_x_ref_t1 FOREIGN KEY (x) REFERENCES public.t1(x),
CONSTRAINT t2_x_fkey FOREIGN KEY (x) REFERENCES public.t1(x),
INDEX t2_x_idx (x ASC),
FAMILY "primary" (y, rowid, x)
)
Expand All @@ -1321,7 +1321,7 @@ t2 CREATE TABLE public.t2 (
x INT8 NULL,
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT t2_pkey PRIMARY KEY (rowid ASC),
CONSTRAINT fk_x_ref_t1 FOREIGN KEY (x) REFERENCES public.t1(x),
CONSTRAINT t2_x_fkey FOREIGN KEY (x) REFERENCES public.t1(x),
INDEX t2_x_idx (x ASC),
FAMILY "primary" (x, rowid)
)
Expand Down Expand Up @@ -1351,7 +1351,7 @@ t2 CREATE TABLE public.t2 (
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
x INT8 NULL DEFAULT 1:::INT8,
CONSTRAINT t2_pkey PRIMARY KEY (rowid ASC),
CONSTRAINT fk_x_ref_t1 FOREIGN KEY (x) REFERENCES public.t1(x),
CONSTRAINT t2_x_fkey FOREIGN KEY (x) REFERENCES public.t1(x),
UNIQUE INDEX t2_x_key (x ASC),
FAMILY "primary" (y, rowid, x)
)
Expand Down Expand Up @@ -1648,9 +1648,9 @@ unique_without_index unique_without_index_c_key UNIQUE UNIQUE (c ASC)
query TTTTB
SHOW CONSTRAINTS FROM uwi_child
----
uwi_child fk_d_e FOREIGN KEY FOREIGN KEY (d, e) REFERENCES unique_without_index(d, e) true
uwi_child fk_d_ref_unique_without_index FOREIGN KEY FOREIGN KEY (d) REFERENCES unique_without_index(d) true
uwi_child fk_e_d FOREIGN KEY FOREIGN KEY (e, d) REFERENCES unique_without_index(e, d) true
uwi_child fk_d_e FOREIGN KEY FOREIGN KEY (d, e) REFERENCES unique_without_index(d, e) true
uwi_child fk_e_d FOREIGN KEY FOREIGN KEY (e, d) REFERENCES unique_without_index(e, d) true
uwi_child uwi_child_d_fkey FOREIGN KEY FOREIGN KEY (d) REFERENCES unique_without_index(d) true

# Attempting to drop a column with a foreign key reference fails.
statement error pq: "unique_d" is referenced by foreign key from table "uwi_child"
Expand Down
Loading

0 comments on commit 44226ec

Please sign in to comment.