From b8a839f570be570cef86b2e2bf218ef503666dd7 Mon Sep 17 00:00:00 2001 From: Youra Cho Date: Wed, 23 Mar 2022 09:16:30 -0700 Subject: [PATCH] executor, infoschema: Add foreign key constraint to infoschema.table_constraints --- executor/infoschema_reader.go | 12 ++++++++++++ infoschema/tables.go | 2 ++ infoschema/tables_test.go | 21 +++++++++++++++++---- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/executor/infoschema_reader.go b/executor/infoschema_reader.go index 8c78c6ac50aec..4a7d212c12942 100644 --- a/executor/infoschema_reader.go +++ b/executor/infoschema_reader.go @@ -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 diff --git a/infoschema/tables.go b/infoschema/tables.go index 6c74d5fd6d670..61a454833f4b6 100644 --- a/infoschema/tables.go +++ b/infoschema/tables.go @@ -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 diff --git a/infoschema/tables_test.go b/infoschema/tables_test.go index f4bcc4037b52d..e705e02448db4 100644 --- a/infoschema/tables_test.go +++ b/infoschema/tables_test.go @@ -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() @@ -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() @@ -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() @@ -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() @@ -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")) +}