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

UpdatedAt set during Create #6001

Open
iain-raidiam opened this issue Jan 26, 2023 · 1 comment
Open

UpdatedAt set during Create #6001

iain-raidiam opened this issue Jan 26, 2023 · 1 comment
Assignees
Labels

Comments

@iain-raidiam
Copy link

Your Question

Why does UpdatedAt get set during a Create, and how can I disable UpdatedAt being set just for a Create

CREATE TABLE examples (
    example varchar(255) not null ,
    created_at                      timestamp not null ,
    updated_at                      timestamp
)
func TestUpdatedAtValueSetDuringCreate(t *testing.T) {
	type Example struct {
		Example   string       `gorm:"type:varchar(255);column:example;not null"`
		CreatedAt sql.NullTime `gorm:"type:timestamp;column:created_at;not null"`
		UpdatedAt sql.NullTime `gorm:"type:timestamp;column:updated_at"`
	}
	newLogger := logger.New(
		log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
		logger.Config{
			SlowThreshold:             time.Second, // Slow SQL threshold
			LogLevel:                  logger.Info, // Log level
			IgnoreRecordNotFoundError: true,        // Ignore ErrRecordNotFound error for logger
			Colorful:                  true,        // Disable color
		},
	)
	gdb, err := gorm.Open(postgres.Open("host=localhost port=5432 user=postgres password=postgres dbname=postgres"), &gorm.Config{Logger: newLogger})
	if err != nil {
		t.Fatalf("unable to create connection: %s", err)
	}
	err = gdb.Create(&Example{Example: "hello world"}).Error
	if err != nil {
		t.Fatalf("unable to create example: %s", err)
	}
}
2023/01/26 17:50:11 /examples/db_test.go:79
[3.674ms] [rows:1] INSERT INTO "examples" ("example","created_at","updated_at") VALUES ('hello world','2023-01-26 17:50:11.457','2023-01-26 17:50:11.457')
--- PASS: TestUpdatedAtValueSetDuringCreate (0.01s)
PASS

The document you expected this should be explained

https://gorm.io/docs/conventions.html#Timestamp-Tracking

Expected answer

This is potentially a bug, or missing documentation/feature.

@iain-raidiam iain-raidiam added the type:question general questions label Jan 26, 2023
Copy link

This issue has been automatically marked as stale because it has been open 360 days with no activity. Remove stale label or comment or this will be closed in 180 days

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants