Skip to content

Commit

Permalink
sql: match postgres FOREIGN KEY constraint name
Browse files Browse the repository at this point in the history
Release note (sql change): Newly created FOREIGN KEYs now have the same
constraint name as postgres - `<table>_<cols>_fkey`. Previously, this
was `fk_<cols>_ref_<target>`.
  • Loading branch information
otan committed Oct 14, 2021
1 parent d38cff4 commit 4bd8180
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
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, "_"))
}
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
1 change: 1 addition & 0 deletions pkg/sql/opt/testutils/testcat/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ go_library(
"//pkg/settings/cluster",
"//pkg/sql/catalog/colinfo",
"//pkg/sql/catalog/descpb",
"//pkg/sql/catalog/tabledesc",
"//pkg/sql/catalog/typedesc",
"//pkg/sql/enum",
"//pkg/sql/oidext",
Expand Down
6 changes: 4 additions & 2 deletions pkg/sql/opt/testutils/testcat/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
"github.com/cockroachdb/cockroach/pkg/sql/opt/cat"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondatapb"
Expand Down Expand Up @@ -422,8 +423,9 @@ func (tc *Catalog) resolveFK(tab *Table, d *tree.ForeignKeyConstraintTableDef) {

constraintName := string(d.Name)
if constraintName == "" {
constraintName = fmt.Sprintf(
"fk_%s_ref_%s", string(d.FromCols[0]), targetTable.TabName.Table(),
constraintName = tabledesc.ForeignKeyConstraintName(
tab.TabName.Table(),
d.FromCols.ToStrings(),
)
}

Expand Down

0 comments on commit 4bd8180

Please sign in to comment.