Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executor, infoschema: Add foreign key constraint to infoschema.table_constraints #33360

Merged
merged 7 commits into from
Mar 25, 2022
12 changes: 12 additions & 0 deletions executor/infoschema_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -1643,6 +1643,18 @@ func (e *memtableRetriever) setDataFromTableConstraints(ctx sessionctx.Context,
)
rows = append(rows, record)
}
// TiDB includes foreign key information for compatibility but foreign keys are not yet enforced.
for _, fk := range tbl.ForeignKeys {
record := types.MakeDatums(
infoschema.CatalogVal, // CONSTRAINT_CATALOG
schema.Name.O, // CONSTRAINT_SCHEMA
fk.Name.O, // CONSTRAINT_NAME
schema.Name.O, // TABLE_SCHEMA
tbl.Name.O, // TABLE_NAME
infoschema.ForeignKeyType, // CONSTRAINT_TYPE
)
rows = append(rows, record)
}
}
}
e.rows = rows
Expand Down
2 changes: 2 additions & 0 deletions infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,8 @@ const (
PrimaryConstraint = "PRIMARY"
// UniqueKeyType is the string constant of UNIQUE.
UniqueKeyType = "UNIQUE"
// ForeignKeyType is the string constant of Foreign Key.
ForeignKeyType = "FOREIGN KEY"
)

// ServerInfo represents the basic server information of single cluster component
Expand Down
21 changes: 17 additions & 4 deletions infoschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ func TestFormatVersion(t *testing.T) {
}
}

// Test statements_summary.
// TestStmtSummaryTable Test statements_summary.
func TestStmtSummaryTable(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
Expand Down Expand Up @@ -1005,7 +1005,7 @@ func TestIssue18845(t *testing.T) {
tk.MustQuery(`select count(*) from information_schema.columns;`)
}

// Test statements_summary_history.
// TestStmtSummaryInternalQuery Test statements_summary_history.
func TestStmtSummaryInternalQuery(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
Expand Down Expand Up @@ -1064,7 +1064,7 @@ func TestStmtSummaryInternalQuery(t *testing.T) {
require.Contains(t, rows[0][0].(string), "Projection")
}

// Test error count and warning count.
// TestStmtSummaryErrorCount Test error count and warning count.
func TestStmtSummaryErrorCount(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
Expand Down Expand Up @@ -1135,7 +1135,7 @@ func TestStmtSummarySensitiveQuery(t *testing.T) {
))
}

// test stmtSummaryEvictedCount
// TestSimpleStmtSummaryEvictedCount test stmtSummaryEvictedCount
func TestSimpleStmtSummaryEvictedCount(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
Expand Down Expand Up @@ -1498,3 +1498,16 @@ func TestReferentialConstraints(t *testing.T) {

tk.MustQuery(`SELECT * FROM information_schema.referential_constraints WHERE table_name='t2'`).Check(testkit.Rows("def referconstraints fk_to_t1 def referconstraints PRIMARY NONE NO ACTION NO ACTION t2 t1"))
}

// TestTableConstraintsContainForeignKeys TiDB Issue: https://github.com/pingcap/tidb/issues/28918
func TestTableConstraintsContainForeignKeys(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("CREATE DATABASE tableconstraints")
tk.MustExec("use tableconstraints")
tk.MustExec("CREATE TABLE `t1` (`id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(25) DEFAULT NULL, PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;")
tk.MustExec("CREATE TABLE `t2` (`id` int(11) NOT NULL AUTO_INCREMENT, `t1_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */, CONSTRAINT `fk_t2_t1` FOREIGN KEY (`t1_id`) REFERENCES `t1` (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;")
tk.MustQuery("SELECT * FROM INFORMATION_SCHEMA.table_constraints WHERE constraint_schema = 'tableconstraints' AND table_name = 't2'").Sort().Check(testkit.Rows("def tableconstraints PRIMARY tableconstraints t2 PRIMARY KEY", "def tableconstraints fk_t2_t1 tableconstraints t2 FOREIGN KEY"))
tk.MustQuery("SELECT * FROM INFORMATION_SCHEMA.table_constraints WHERE constraint_schema = 'tableconstraints' AND table_name = 't1'").Sort().Check(testkit.Rows("def tableconstraints PRIMARY tableconstraints t1 PRIMARY KEY"))
}