Skip to content

Field time.Time doesn't get updated when saving a list of models #7540

@mosceo

Description

@mosceo

GORM Playground Link

go-gorm/playground#821

Description

A model has time.Time field with a default tag. When saving a list of these models, that time.Time field doesn't get updated (you can see it in the SQL). After removing the default tag, it gets updated correctly. Also if saving just one model (not a list), it also gets updated.

This problem doesn't happen with int field, that also has a default tag.

type Price struct {
	ProductId int       `gorm:"primaryKey"`
	Price     int       `gorm:"default:1"`
	SomeTime  time.Time `gorm:"default:'2015-10-22T14:00:00Z'"`
}

func main() {
	prices := []Price{
		{ProductId: 1, Price: 100, SomeTime: time.Now()},
		{ProductId: 2, Price: 150, SomeTime: time.Now()},
	}

	// Will correctly create two rows
	db.Debug().Save(prices)

	// Will NOT update some_time field (check SQL below)
	//
	// [1.647ms] [rows:2] INSERT INTO "prices" ("price","some_time","product_id") VALUES
	//                    (100,'2015-10-23 00:00:00',1),(150,'2024-06-06 21:42:19.527',2) ON CONFLICT ("product_id")
	//                    DO UPDATE SET "price"="excluded"."price" RETURNING "some_time","product_id"
	prices[1].SomeTime = time.Now().Add(-10000 * time.Hour)
	db.Debug().Save(prices)
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions