Skip to content

Commit

Permalink
sql: SHOW CREATE TABLE treated unique constraints like unique indexes
Browse files Browse the repository at this point in the history
```
root@localhost:26257/test> CREATE TABLE t (i INT PRIMARY KEY, j INT);
CREATE TABLE
root@localhost:26257/test> ALTER TABLE t ADD CONSTRAINT c UNIQUE (j);
ALTER TABLE
root@localhost:26257/test> CREATE UNIQUE INDEX idx ON t (j);
CREATE INDEX
```

Before:
```
root@localhost:26257/test> SHOW CREATE TABLE t;
  table_name |               create_statement
-------------+------------------------------------------------
  t          | CREATE TABLE public.t (
             |     i INT8 NOT NULL,
             |     j INT8 NULL,
             |     CONSTRAINT "primary" PRIMARY KEY (i ASC),
             |     UNIQUE INDEX c (j ASC),
             |     UNIQUE INDEX idx (j ASC),
             |     FAMILY "primary" (i, j)
             | )
```

After:
```
[email protected]:55792/movr> SHOW CREATE TABLE t;
  table_name |               create_statement
-------------+------------------------------------------------
  t          | CREATE TABLE public.t (
             |     i INT8 NOT NULL,
             |     j INT8 NULL,
             |     CONSTRAINT "primary" PRIMARY KEY (i ASC),
             |     UNIQUE INDEX idx (j ASC),
             |     FAMILY "primary" (i, j),
             |     CONSTRAINT c UNIQUE (j)
             | )
```

Release note (bug fix): UNIQUE constraints were displayed as UNIQUE INDEX
entries in SHOW CREATE TABLE.
  • Loading branch information
ajwerner committed May 28, 2021
1 parent 7e8a568 commit f10d2b6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/sql/show_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ func ShowCreateTable(
// clauses as well.
includeInterleaveClause = true
}
// Don't show indexes which represent unique constraints.
if idx.IsUnique() && !idx.IsCreatedExplicitly() {
continue
}
if !idx.Primary() && includeInterleaveClause {
// Showing the primary index is handled above.

Expand Down
22 changes: 22 additions & 0 deletions pkg/sql/show_create_clauses.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,28 @@ func showConstraintClause(
f.WriteString(" NOT VALID")
}
}
for _, idx := range desc.AllIndexes() {
if !idx.IsUnique() || idx.IsCreatedExplicitly() || idx.Primary() {
continue
}
f.WriteString(",\n\t")
if len(idx.GetName()) > 0 {
f.WriteString("CONSTRAINT ")
formatQuoteNames(&f.Buffer, idx.GetName())
f.WriteString(" ")
}
f.WriteString("UNIQUE ")
f.WriteString("(")
startIdx := idx.ExplicitColumnStartIdx()
for i := startIdx; i < idx.NumColumns(); i++ {
if i > startIdx {
f.WriteString(", ")
}
n := idx.GetColumnName(i)
f.FormatNameP(&n)
}
f.WriteString(")")
}
f.WriteString("\n)")
return nil
}

0 comments on commit f10d2b6

Please sign in to comment.