Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jsonb field of association isn't properly saved if it has default specified #6418

Open
jivot opened this issue Jun 16, 2023 · 0 comments · May be fixed by #6434
Open

Jsonb field of association isn't properly saved if it has default specified #6418

jivot opened this issue Jun 16, 2023 · 0 comments · May be fixed by #6434
Assignees
Labels
type:with reproduction steps with reproduction steps

Comments

@jivot
Copy link

jivot commented Jun 16, 2023

GORM Playground Link

go-gorm/playground#620

Description

type ValueDep struct {
	gorm.Model

	ID      int `gorm:"primarykey,autoIncrement"`
	ValueID int
	Name    string `gorm:"column:name; default:'default'"`
	Params  Params `gorm:"column:params; type:jsonb; default:'{}'"`
}

type Value struct {
	gorm.Model

	ID   int         `gorm:"primarykey,autoIncrement"`
	Deps []*ValueDep `gorm:"foreignKey:ValueID"`
}

type Params map[string]string

func (p *Params) Scan(val interface{}) error {
	switch v := val.(type) {
	case []byte:
		return json.Unmarshal(v, p)
	case string:
		return json.Unmarshal([]byte(v), p)
	default:
		return fmt.Errorf("unsupported type: %T", v)
	}
}

func (p Params) Value() (driver.Value, error) {
	return json.Marshal(p)
}

Steps to reproduce:

var value Value
// read value from the db

// assign new values to the map
value.Deps[0].Params["foo"] = "new-bar"

// save the struct to the db
DB.Session(&gorm.Session{FullSaveAssociations: true}).Save(value)

SQL which is supposed to update association will look like this

INSERT INTO "value_deps" ("created_at","updated_at","deleted_at","value_id","name","params","id") VALUES ('2023-06-16 20:10:28.427','2023-06-16 20:10:28.444',NULL,11,'new-name','{"foo":"new-bar"}',11) ON CONFLICT ("id") DO UPDATE SET "updated_at"='2023-06-16 20:10:28.444',"deleted_at"="excluded"."deleted_at","value_id"="excluded"."value_id","name"="excluded"."name" RETURNING "id","params","id"

Notice, that the params field isn't listed in the SET clause. Which means that this sql will work correctly only if we're inserting a new association. If we're trying to update an existing one the new-bar value will be lost.

This happens only if we specify default:'{}' option. Without it the save will work ok

@github-actions github-actions bot added the type:with reproduction steps with reproduction steps label Jun 16, 2023
@black-06 black-06 linked a pull request Jun 28, 2023 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:with reproduction steps with reproduction steps
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants