Disabled updates to the primary key using cockroachdb
This commit is contained in:
parent
a80bfb8bc5
commit
039a759a53
@ -43,6 +43,11 @@ func Update(scope *Scope) {
|
||||
|
||||
if updateAttrs, ok := scope.InstanceGet("gorm:update_attrs"); ok {
|
||||
for key, value := range updateAttrs.(map[string]interface{}) {
|
||||
if !scope.Dialect().SupportUpdatePrimaryKey() {
|
||||
if field, ok := scope.Fields()[key]; ok && field.IsPrimaryKey {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if scope.changeableDBColumn(key) {
|
||||
sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(key), scope.AddToVars(value)))
|
||||
}
|
||||
@ -50,6 +55,9 @@ func Update(scope *Scope) {
|
||||
} else {
|
||||
fields := scope.Fields()
|
||||
for _, field := range fields {
|
||||
if field.IsPrimaryKey && !scope.Dialect().SupportUpdatePrimaryKey() {
|
||||
continue
|
||||
}
|
||||
if scope.changeableField(field) && !field.IsPrimaryKey && field.IsNormal {
|
||||
sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface())))
|
||||
} else if relationship := field.Relationship; relationship != nil && relationship.Kind == "belongs_to" {
|
||||
|
@ -22,6 +22,10 @@ func (cockroach) SupportUniquePrimaryKey() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (cockroach) SupportUpdatePrimaryKey() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (cockroach) NewUniqueKey(scope *Scope) uint64 {
|
||||
rows, err := scope.NewDB().Raw(`SELECT experimental_unique_int()`).Rows()
|
||||
if err != nil {
|
||||
|
@ -20,6 +20,10 @@ func (commonDialect) SupportUniquePrimaryKey() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (commonDialect) SupportUpdatePrimaryKey() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (commonDialect) NewUniqueKey(scope *Scope) uint64 {
|
||||
panic("NewUniqueKey not supported by commonDialect")
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ type Dialect interface {
|
||||
BinVar(i int) string
|
||||
SupportLastInsertId() bool
|
||||
SupportUniquePrimaryKey() bool
|
||||
SupportUpdatePrimaryKey() bool
|
||||
NewUniqueKey(scope *Scope) uint64
|
||||
HasTop() bool
|
||||
SqlTag(value reflect.Value, size int, autoIncrease bool) string
|
||||
|
Loading…
x
Reference in New Issue
Block a user