Skip to content

Commit

Permalink
Merge pull request cockroachdb#13938 from mjibson/show-create-refs
Browse files Browse the repository at this point in the history
sql: show foreign key refs in SHOW CREATE TABLE
  • Loading branch information
maddyblue authored Mar 7, 2017
2 parents b9849a2 + 26288fe commit e0f3b01
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
31 changes: 24 additions & 7 deletions pkg/sql/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,13 +374,30 @@ func (p *planner) showCreateTable(tn parser.Name, desc *sqlbase.TableDescriptor)
if err != nil {
return "", err
}
fmt.Fprintf(&buf, ",\n\t%sINDEX %s (%s)%s%s",
isUnique[idx.Unique],
quoteNames(idx.Name),
makeIndexColNames(idx),
storing,
interleave,
)
if fk := idx.ForeignKey; fk.IsSet() {
fkTable, err := p.getTableLeaseByID(fk.Table)
if err != nil {
return "", err
}
fkIdx, err := fkTable.FindIndexByID(fk.Index)
if err != nil {
return "", err
}
fmt.Fprintf(&buf, ",\n\tCONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)",
parser.Name(fk.Name),
quoteNames(idx.ColumnNames...),
parser.Name(fkTable.Name),
quoteNames(fkIdx.ColumnNames...),
)
} else {
fmt.Fprintf(&buf, ",\n\t%sINDEX %s (%s)%s%s",
isUnique[idx.Unique],
quoteNames(idx.Name),
makeIndexColNames(idx),
storing,
interleave,
)
}
}
for _, fam := range desc.Families {
activeColumnNames := make([]string, 0, len(fam.ColumnNames))
Expand Down
22 changes: 22 additions & 0 deletions pkg/sql/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ func TestShowCreateTable(t *testing.T) {
if _, err := sqlDB.Exec(`
CREATE DATABASE d;
SET DATABASE = d;
CREATE TABLE items (
a int,
b int,
c int unique,
primary key (a, b)
);
`); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -144,6 +150,22 @@ func TestShowCreateTable(t *testing.T) {
b INT NULL,
INDEX c (a ASC, b DESC),
FAMILY "primary" (a, b, rowid)
)`,
},
{
stmt: `CREATE TABLE %s (
i int,
j int,
FOREIGN KEY (i, j) REFERENCES items (a, b),
k int REFERENCES items (c)
)`,
expect: `CREATE TABLE %s (
i INT NULL,
j INT NULL,
k INT NULL,
CONSTRAINT fk_i_ref_items FOREIGN KEY (i, j) REFERENCES items (a, b),
CONSTRAINT fk_k_ref_items FOREIGN KEY (k) REFERENCES items (c),
FAMILY "primary" (i, j, k, rowid)
)`,
},
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/sql/testdata/fk
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,19 @@ CREATE TABLE delivery (
INDEX (item)
)

query TT
SHOW CREATE TABLE delivery
----
delivery CREATE TABLE delivery (
ts TIMESTAMP NULL DEFAULT now(),
"order" INT NULL,
shipment INT NULL,
item STRING NULL,
CONSTRAINT fk_item_ref_products FOREIGN KEY (item) REFERENCES products (upc),
CONSTRAINT fk_order_ref_orders FOREIGN KEY ("order", shipment) REFERENCES orders (id, shipment),
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')
Expand Down

0 comments on commit e0f3b01

Please sign in to comment.