diff --git a/README.md b/README.md index 03fbee15..dbfbedd5 100644 --- a/README.md +++ b/README.md @@ -138,12 +138,14 @@ db.SingularTable(true) ```go // Create table db.CreateTable(&User{}) +db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&User{}) // Drop table db.DropTable(&User{}) // Automating Migration db.AutoMigrate(&User{}) +db.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(&User{}) db.AutoMigrate(&User{}, &Product{}, &Order{}) // Feel free to change your struct, AutoMigrate will keep your database up-to-date. // AutoMigrate will ONLY add *new columns* and *new indexes*, diff --git a/scope_private.go b/scope_private.go index 4344f22e..1d906e76 100644 --- a/scope_private.go +++ b/scope_private.go @@ -465,6 +465,17 @@ func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope { return scope } +/** + Return the table options string or an empty string if the table options does not exist +*/ +func (scope *Scope) getTableOptions() string{ + tableOptions, ok := scope.Get("gorm:table_options") + if !ok { + return "" + } + return tableOptions.(string) +} + func (scope *Scope) createJoinTable(field *StructField) { if relationship := field.Relationship; relationship != nil && relationship.JoinTableHandler != nil { joinTableHandler := relationship.JoinTableHandler @@ -488,8 +499,7 @@ func (scope *Scope) createJoinTable(field *StructField) { sqlTypes = append(sqlTypes, scope.Quote(relationship.AssociationForeignDBNames[idx])+" "+primaryKeySqlType) } } - - scope.Err(scope.NewDB().Exec(fmt.Sprintf("CREATE TABLE %v (%v)", scope.Quote(joinTable), strings.Join(sqlTypes, ","))).Error) + scope.Err(scope.NewDB().Exec(fmt.Sprintf("CREATE TABLE %v (%v) %s", scope.Quote(joinTable), strings.Join(sqlTypes, ","), scope.getTableOptions())).Error) } scope.NewDB().Table(joinTable).AutoMigrate(joinTableHandler) } @@ -514,7 +524,7 @@ func (scope *Scope) createTable() *Scope { if len(primaryKeys) > 0 { primaryKeyStr = fmt.Sprintf(", PRIMARY KEY (%v)", strings.Join(primaryKeys, ",")) } - scope.Raw(fmt.Sprintf("CREATE TABLE %v (%v %v)", scope.QuotedTableName(), strings.Join(tags, ","), primaryKeyStr)).Exec() + scope.Raw(fmt.Sprintf("CREATE TABLE %v (%v %v) %s", scope.QuotedTableName(), strings.Join(tags, ","), primaryKeyStr, scope.getTableOptions())).Exec() return scope }