Disabled updates to the primary key using cockroachdb

This commit is contained in:
Tristan Rice 2016-01-29 14:11:04 -08:00
parent a80bfb8bc5
commit 039a759a53
4 changed files with 17 additions and 0 deletions

View File

@ -43,6 +43,11 @@ func Update(scope *Scope) {
if updateAttrs, ok := scope.InstanceGet("gorm:update_attrs"); ok { if updateAttrs, ok := scope.InstanceGet("gorm:update_attrs"); ok {
for key, value := range updateAttrs.(map[string]interface{}) { 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) { if scope.changeableDBColumn(key) {
sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(key), scope.AddToVars(value))) sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(key), scope.AddToVars(value)))
} }
@ -50,6 +55,9 @@ func Update(scope *Scope) {
} else { } else {
fields := scope.Fields() fields := scope.Fields()
for _, field := range fields { for _, field := range fields {
if field.IsPrimaryKey && !scope.Dialect().SupportUpdatePrimaryKey() {
continue
}
if scope.changeableField(field) && !field.IsPrimaryKey && field.IsNormal { if scope.changeableField(field) && !field.IsPrimaryKey && field.IsNormal {
sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface()))) 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" { } else if relationship := field.Relationship; relationship != nil && relationship.Kind == "belongs_to" {

View File

@ -22,6 +22,10 @@ func (cockroach) SupportUniquePrimaryKey() bool {
return false return false
} }
func (cockroach) SupportUpdatePrimaryKey() bool {
return false
}
func (cockroach) NewUniqueKey(scope *Scope) uint64 { func (cockroach) NewUniqueKey(scope *Scope) uint64 {
rows, err := scope.NewDB().Raw(`SELECT experimental_unique_int()`).Rows() rows, err := scope.NewDB().Raw(`SELECT experimental_unique_int()`).Rows()
if err != nil { if err != nil {

View File

@ -20,6 +20,10 @@ func (commonDialect) SupportUniquePrimaryKey() bool {
return true return true
} }
func (commonDialect) SupportUpdatePrimaryKey() bool {
return true
}
func (commonDialect) NewUniqueKey(scope *Scope) uint64 { func (commonDialect) NewUniqueKey(scope *Scope) uint64 {
panic("NewUniqueKey not supported by commonDialect") panic("NewUniqueKey not supported by commonDialect")
} }

View File

@ -9,6 +9,7 @@ type Dialect interface {
BinVar(i int) string BinVar(i int) string
SupportLastInsertId() bool SupportLastInsertId() bool
SupportUniquePrimaryKey() bool SupportUniquePrimaryKey() bool
SupportUpdatePrimaryKey() bool
NewUniqueKey(scope *Scope) uint64 NewUniqueKey(scope *Scope) uint64
HasTop() bool HasTop() bool
SqlTag(value reflect.Value, size int, autoIncrease bool) string SqlTag(value reflect.Value, size int, autoIncrease bool) string