Added support of field gorm options drop_primary_keys and drop_unique_index
This commit is contained in:
parent
81ceeb3e9f
commit
4019c14651
@ -1177,8 +1177,8 @@ db.Where("email = ?", "x@example.org").Attrs(User{RegisteredIp: "111.111.111.111
|
||||
```go
|
||||
type Delta struct {
|
||||
Id int;
|
||||
Was interface{} `gorm:"embedded:prefixed"`;
|
||||
Became interface{} `gorm:"embedded:prefixed"`;
|
||||
Was interface{} `gorm:"embedded:prefixed;drop_primary_keys;drop_unique_index"`;
|
||||
Became interface{} `gorm:"embedded:prefixed;drop_primary_keys;drop_unique_index"`;
|
||||
}
|
||||
|
||||
type Login struct {
|
||||
|
@ -34,6 +34,8 @@ type StructField struct {
|
||||
Tag reflect.StructTag
|
||||
Struct reflect.StructField
|
||||
IsForeignKey bool
|
||||
IgnoreUniqueIndex bool
|
||||
IgnorePrimaryKey bool
|
||||
Relationship *Relationship
|
||||
Value reflect.Value
|
||||
}
|
||||
@ -307,7 +309,14 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||
}
|
||||
toField.Names = append([]string{fieldStruct.Name}, toField.Names...)
|
||||
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)
|
||||
}
|
||||
}
|
||||
@ -381,7 +390,10 @@ func (scope *Scope) generateSqlTag(field *StructField) string {
|
||||
sqlType = value
|
||||
}
|
||||
|
||||
additionalType := sqlSettings["NOT NULL"] + " " + sqlSettings["UNIQUE"]
|
||||
additionalType := sqlSettings["NOT NULL"]
|
||||
if !field.IgnoreUniqueIndex {
|
||||
additionalType = " " + sqlSettings["UNIQUE"]
|
||||
}
|
||||
if value, ok := sqlSettings["DEFAULT"]; ok {
|
||||
additionalType = additionalType + " DEFAULT " + value
|
||||
}
|
||||
|
@ -474,7 +474,7 @@ func (scope *Scope) createTable() *Scope {
|
||||
tags = append(tags, scope.Quote(field.DBName)+" "+sqlTag)
|
||||
}
|
||||
|
||||
if field.IsPrimaryKey {
|
||||
if field.IsPrimaryKey && !field.IgnorePrimaryKey {
|
||||
primaryKeys = append(primaryKeys, field.DBName)
|
||||
}
|
||||
scope.createJoinTable(field)
|
||||
@ -572,6 +572,7 @@ func (scope *Scope) autoIndex() *Scope {
|
||||
indexes[name] = append(indexes[name], field.DBName)
|
||||
}
|
||||
|
||||
if field.IgnoreUniqueIndex {
|
||||
if name, ok := sqlSettings["UNIQUE_INDEX"]; ok {
|
||||
if name == "UNIQUE_INDEX" {
|
||||
name = fmt.Sprintf("uix_%v_%v", scope.TableName(), field.DBName)
|
||||
@ -579,6 +580,7 @@ func (scope *Scope) autoIndex() *Scope {
|
||||
uniqueIndexes[name] = append(uniqueIndexes[name], field.DBName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for name, columns := range indexes {
|
||||
scope.addIndex(false, name, columns...)
|
||||
|
Loading…
x
Reference in New Issue
Block a user