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

fix: quote index when creating table #5331

Merged
merged 1 commit into from May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion migrator/migrator.go
Expand Up @@ -223,7 +223,7 @@ func (m Migrator) CreateTable(values ...interface{}) error {
}

createTableSQL += ","
values = append(values, clause.Expr{SQL: idx.Name}, tx.Migrator().(BuildIndexOptionsInterface).BuildIndexOptions(idx.Fields, stmt))
values = append(values, clause.Column{Name: idx.Name}, tx.Migrator().(BuildIndexOptionsInterface).BuildIndexOptions(idx.Fields, stmt))
}
}

Expand Down
19 changes: 19 additions & 0 deletions tests/migrate_test.go
Expand Up @@ -262,6 +262,25 @@ func TestMigrateTable(t *testing.T) {
}
}

func TestMigrateWithQuotedIndex(t *testing.T) {
if DB.Dialector.Name() != "mysql" {
t.Skip()
}

type QuotedIndexStruct struct {
gorm.Model
Name string `gorm:"size:255;index:AS"` // AS is one of MySQL reserved words
}

if err := DB.Migrator().DropTable(&QuotedIndexStruct{}); err != nil {
t.Fatalf("Failed to drop table, got error %v", err)
}

if err := DB.AutoMigrate(&QuotedIndexStruct{}); err != nil {
t.Fatalf("Failed to auto migrate, but got error %v", err)
}
}

func TestMigrateIndexes(t *testing.T) {
type IndexStruct struct {
gorm.Model
Expand Down