Added support of field gorm options drop_primary_keys and drop_unique_index

This commit is contained in:
Dmitry Yu Okunev 2015-05-22 22:02:53 +03:00
parent 81ceeb3e9f
commit 4019c14651
3 changed files with 23 additions and 9 deletions

View File

@ -1177,8 +1177,8 @@ db.Where("email = ?", "x@example.org").Attrs(User{RegisteredIp: "111.111.111.111
```go ```go
type Delta struct { type Delta struct {
Id int; Id int;
Was interface{} `gorm:"embedded:prefixed"`; Was interface{} `gorm:"embedded:prefixed;drop_primary_keys;drop_unique_index"`;
Became interface{} `gorm:"embedded:prefixed"`; Became interface{} `gorm:"embedded:prefixed;drop_primary_keys;drop_unique_index"`;
} }
type Login struct { type Login struct {

View File

@ -34,6 +34,8 @@ type StructField struct {
Tag reflect.StructTag Tag reflect.StructTag
Struct reflect.StructField Struct reflect.StructField
IsForeignKey bool IsForeignKey bool
IgnoreUniqueIndex bool
IgnorePrimaryKey bool
Relationship *Relationship Relationship *Relationship
Value reflect.Value Value reflect.Value
} }
@ -307,7 +309,14 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
} }
toField.Names = append([]string{fieldStruct.Name}, toField.Names...) toField.Names = append([]string{fieldStruct.Name}, toField.Names...)
modelStruct.StructFields = append(modelStruct.StructFields, toField) modelStruct.StructFields = append(modelStruct.StructFields, toField)
if toField.IsPrimaryKey { if _, ok := gormSettings["DROP_UNIQUE_INDEX"]; ok {
toField.IgnoreUniqueIndex = true
}
if _, ok := gormSettings["DROP_PRIMARY_KEYS"]; ok {
toField.IgnorePrimaryKey = true
}
if toField.IsPrimaryKey && !toField.IgnorePrimaryKey {
modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, toField) modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, toField)
} }
} }
@ -381,7 +390,10 @@ func (scope *Scope) generateSqlTag(field *StructField) string {
sqlType = value sqlType = value
} }
additionalType := sqlSettings["NOT NULL"] + " " + sqlSettings["UNIQUE"] additionalType := sqlSettings["NOT NULL"]
if !field.IgnoreUniqueIndex {
additionalType = " " + sqlSettings["UNIQUE"]
}
if value, ok := sqlSettings["DEFAULT"]; ok { if value, ok := sqlSettings["DEFAULT"]; ok {
additionalType = additionalType + " DEFAULT " + value additionalType = additionalType + " DEFAULT " + value
} }

View File

@ -474,7 +474,7 @@ func (scope *Scope) createTable() *Scope {
tags = append(tags, scope.Quote(field.DBName)+" "+sqlTag) tags = append(tags, scope.Quote(field.DBName)+" "+sqlTag)
} }
if field.IsPrimaryKey { if field.IsPrimaryKey && !field.IgnorePrimaryKey {
primaryKeys = append(primaryKeys, field.DBName) primaryKeys = append(primaryKeys, field.DBName)
} }
scope.createJoinTable(field) scope.createJoinTable(field)
@ -572,11 +572,13 @@ func (scope *Scope) autoIndex() *Scope {
indexes[name] = append(indexes[name], field.DBName) indexes[name] = append(indexes[name], field.DBName)
} }
if name, ok := sqlSettings["UNIQUE_INDEX"]; ok { if field.IgnoreUniqueIndex {
if name == "UNIQUE_INDEX" { if name, ok := sqlSettings["UNIQUE_INDEX"]; ok {
name = fmt.Sprintf("uix_%v_%v", scope.TableName(), field.DBName) if name == "UNIQUE_INDEX" {
name = fmt.Sprintf("uix_%v_%v", scope.TableName(), field.DBName)
}
uniqueIndexes[name] = append(uniqueIndexes[name], field.DBName)
} }
uniqueIndexes[name] = append(uniqueIndexes[name], field.DBName)
} }
} }