fix: quote index when creating table (#5331)
This commit is contained in:
		
							parent
							
								
									373bcf7aca
								
							
						
					
					
						commit
						f5e77aab2f
					
				| @ -223,7 +223,7 @@ func (m Migrator) CreateTable(values ...interface{}) error { | |||||||
| 					} | 					} | ||||||
| 
 | 
 | ||||||
| 					createTableSQL += "," | 					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)) | ||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -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) { | func TestMigrateIndexes(t *testing.T) { | ||||||
| 	type IndexStruct struct { | 	type IndexStruct struct { | ||||||
| 		gorm.Model | 		gorm.Model | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 black-06
						black-06