Merge pull request #572 from eirmag/master

Table suffix to create tables with InnoDB engine
This commit is contained in:
Jinzhu 2015-08-02 07:28:42 +08:00
commit 0def184b0c
2 changed files with 15 additions and 3 deletions

View File

@ -138,12 +138,14 @@ db.SingularTable(true)
```go ```go
// Create table // Create table
db.CreateTable(&User{}) db.CreateTable(&User{})
db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&User{})
// Drop table // Drop table
db.DropTable(&User{}) db.DropTable(&User{})
// Automating Migration // Automating Migration
db.AutoMigrate(&User{}) db.AutoMigrate(&User{})
db.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(&User{})
db.AutoMigrate(&User{}, &Product{}, &Order{}) db.AutoMigrate(&User{}, &Product{}, &Order{})
// Feel free to change your struct, AutoMigrate will keep your database up-to-date. // Feel free to change your struct, AutoMigrate will keep your database up-to-date.
// AutoMigrate will ONLY add *new columns* and *new indexes*, // AutoMigrate will ONLY add *new columns* and *new indexes*,

View File

@ -465,6 +465,17 @@ func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope {
return 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) { func (scope *Scope) createJoinTable(field *StructField) {
if relationship := field.Relationship; relationship != nil && relationship.JoinTableHandler != nil { if relationship := field.Relationship; relationship != nil && relationship.JoinTableHandler != nil {
joinTableHandler := relationship.JoinTableHandler joinTableHandler := relationship.JoinTableHandler
@ -488,8 +499,7 @@ func (scope *Scope) createJoinTable(field *StructField) {
sqlTypes = append(sqlTypes, scope.Quote(relationship.AssociationForeignDBNames[idx])+" "+primaryKeySqlType) sqlTypes = append(sqlTypes, scope.Quote(relationship.AssociationForeignDBNames[idx])+" "+primaryKeySqlType)
} }
} }
scope.Err(scope.NewDB().Exec(fmt.Sprintf("CREATE TABLE %v (%v) %s", scope.Quote(joinTable), strings.Join(sqlTypes, ","), scope.getTableOptions())).Error)
scope.Err(scope.NewDB().Exec(fmt.Sprintf("CREATE TABLE %v (%v)", scope.Quote(joinTable), strings.Join(sqlTypes, ","))).Error)
} }
scope.NewDB().Table(joinTable).AutoMigrate(joinTableHandler) scope.NewDB().Table(joinTable).AutoMigrate(joinTableHandler)
} }
@ -514,7 +524,7 @@ func (scope *Scope) createTable() *Scope {
if len(primaryKeys) > 0 { if len(primaryKeys) > 0 {
primaryKeyStr = fmt.Sprintf(", PRIMARY KEY (%v)", strings.Join(primaryKeys, ",")) 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 return scope
} }