From 4019c14651716e3c5a85ed44986a4b09bd3c0d52 Mon Sep 17 00:00:00 2001 From: Dmitry Yu Okunev Date: Fri, 22 May 2015 22:02:53 +0300 Subject: [PATCH] Added support of field gorm options drop_primary_keys and drop_unique_index --- README.md | 4 ++-- model_struct.go | 16 ++++++++++++++-- scope_private.go | 12 +++++++----- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 78e6e458..bf69a204 100644 --- a/README.md +++ b/README.md @@ -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 { diff --git a/model_struct.go b/model_struct.go index 0a1631a9..39ca525c 100644 --- a/model_struct.go +++ b/model_struct.go @@ -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 } diff --git a/scope_private.go b/scope_private.go index 4ecefe3a..016244c9 100644 --- a/scope_private.go +++ b/scope_private.go @@ -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,11 +572,13 @@ func (scope *Scope) autoIndex() *Scope { indexes[name] = append(indexes[name], field.DBName) } - if name, ok := sqlSettings["UNIQUE_INDEX"]; ok { - if name == "UNIQUE_INDEX" { - name = fmt.Sprintf("uix_%v_%v", scope.TableName(), 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) + } + uniqueIndexes[name] = append(uniqueIndexes[name], field.DBName) } - uniqueIndexes[name] = append(uniqueIndexes[name], field.DBName) } }