Skip to content

default:null columns do not update with clause.OnConflict{UpdateAll: true} #4776

@ghost

Description

Your Question

I ran into an issue where I was unable to update users records with a model similar to this (basically, the user record wouldnt apply the new data to the column):

type User struct {
        Firstname        string     `gorm:"not null;column:firstname" json:"firstname"`
	Lastname         string     `gorm:"default:null;column:lastname" json:"lastname"`
	Username         string     `gorm:"default:null;column:username" json:"username"`
	Email            string     `gorm:"not null;column:email" json:"email"`
}

The updates were done as follows:

	for i := range users {
		db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&users[i])
	}

Upon changing to:

type User struct {
	Firstname        string     `gorm:"not null;column:firstname" json:"firstname"`
	Lastname         string     `gorm:"not null;column:lastname" json:"lastname"`
	Username         string     `gorm:"not null;column:username" json:"username"`
	Email            string     `gorm:"not null;column:email" json:"email"`
}

Changing default:null to not null for affected columns corrected the issue. It's as if the default:null columns were not actually being updated with OnConflict. The initial issue was that we noticed that username changes were not being updated or upserted properly. Is this by design or a bug?

GORM version that I'm using is gorm.io/gorm v1.21.10 with mysql

The document you expected this should be explained

https://gorm.io/docs/create.html#Upsert-On-Conflict

Expected answer

Just like it says in the above link:

Update all columns, except primary keys, to new value on conflict

The columns that were default:null were not updated unless they were changed to not null.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions