Skip to content

Commit 0787da1

Browse files
committed
schemadiff: allow char->varchar FK reference type matching
Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com>
1 parent c91f57b commit 0787da1

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

go/vt/schemadiff/schema.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,9 @@ func (s *Schema) normalize() error {
357357
if child.Type == "char" && parent.Type == "varchar" {
358358
return true
359359
}
360+
if child.Type == "varchar" && parent.Type == "char" {
361+
return true
362+
}
360363
return false
361364
}
362365
colTypeEqualForForeignKey := func(child, parent *sqlparser.ColumnType) bool {

go/vt/schemadiff/schema_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,13 +383,12 @@ func TestInvalidSchema(t *testing.T) {
383383
schema: "create table t10(id varchar(50) charset utf8mb4 collate utf8mb4_0900_ai_ci primary key); create table t11 (id int primary key, i varchar(100) charset utf8mb4 collate utf8mb4_0900_ai_ci, key ix(i), constraint f10 foreign key (i) references t10(id) on delete restrict)",
384384
},
385385
{
386-
// weirdly allowed: varchar->char
386+
// allowed: varchar->char
387387
schema: "create table t10(id varchar(50) charset utf8mb4 collate utf8mb4_0900_ai_ci primary key); create table t11 (id int primary key, i char(100) charset utf8mb4 collate utf8mb4_0900_ai_ci, key ix(i), constraint f10 foreign key (i) references t10(id) on delete restrict)",
388388
},
389389
{
390-
// but weirdly not allowed: char->varchar
391-
schema: "create table t10(id char(50) charset utf8mb4 collate utf8mb4_0900_ai_ci primary key); create table t11 (id int primary key, i varchar(50) charset utf8mb4 collate utf8mb4_0900_ai_ci, key ix(i), constraint f10 foreign key (i) references t10(id) on delete restrict)",
392-
expectErr: &ForeignKeyColumnTypeMismatchError{Table: "t11", Constraint: "f10", Column: "i", ReferencedTable: "t10", ReferencedColumn: "id"},
390+
// allowed: char->varchar
391+
schema: "create table t10(id char(50) charset utf8mb4 collate utf8mb4_0900_ai_ci primary key); create table t11 (id int primary key, i varchar(50) charset utf8mb4 collate utf8mb4_0900_ai_ci, key ix(i), constraint f10 foreign key (i) references t10(id) on delete restrict)",
393392
},
394393
{
395394
schema: "create table t10(id varchar(50) charset utf8mb3 primary key); create table t11 (id int primary key, i varchar(100) charset utf8mb4, key ix(i), constraint f10 foreign key (i) references t10(id) on delete restrict)",

0 commit comments

Comments
 (0)