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

NewUpdate SET ignores nullzero #339

Closed
sergolius opened this issue Nov 30, 2021 · 5 comments
Closed

NewUpdate SET ignores nullzero #339

sergolius opened this issue Nov 30, 2021 · 5 comments
Labels
invalid This doesn't seem right

Comments

@sergolius
Copy link

sergolius commented Nov 30, 2021

nullzero behaves differently at NewInsert, NewUpdate.
OmitZero() has no impact on NewUpdate that uses Set method.

NewInsert will produce SQL where nullzero values replaced with DEFAULT, but NewUpdate will replace with default value of type.

type User struct {
	ID          uuid.UUID `bun:"type:uuid,default:uuid_generate_v4()"`
	FirstName   string    `bun:"first_name,nullzero"`
	ProfileID   uuid.UUID `bun:"profile_id,nullzero,type:uuid"`
	DeletedAt   time.Time `bun:"deleted_at,nullzero"`
	DocumentIDs []string  `bun:"document_ids,nullzero"`
}
user := new(User)
_, err := db.NewInsert().Model(user).
	Returning("null").
	Exec(ctx)
INSERT INTO "users" ("id", "first_name", "profile_id", "deleted_at", "document_ids") VALUES (DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT)

Set ignores nullzero:

user = new(User)
_, err = db.NewUpdate().
	Model(user).
	Set("first_name = ?first_name").
	Set("profile_id = ?profile_id").
	Set("deleted_at = ?deleted_at").
	Set("document_ids = ?document_ids").
	Where("true").
	Exec(ctx)
UPDATE "users" AS "user" SET first_name = '', profile_id = '00000000-0000-0000-0000-000000000000', deleted_at = '0001-01-01 00:00:00+00:00', document_ids = 'null' WHERE (true)

Column applies nullzero:

user = new(User)
_, err = db.NewUpdate().
	Model(user).
	Column("first_name").
	Column("profile_id").
	Column("deleted_at").
	Column("document_ids").
	Where("true").
	Exec(ctx)
UPDATE "users" AS "user" SET "first_name" = NULL, "profile_id" = NULL, "deleted_at" = NULL, "document_ids" = NULL WHERE (true)
@sergolius
Copy link
Author

It requires explicit pointer using

*string
*uuid.UUID
*time.Time

@vmihailenco
Copy link
Member

OmitZero() has no impact on NewUpdate that uses Set method.

This is working as expected. OmitZero only works with updates and only when using Column method.

@vmihailenco vmihailenco added the invalid This doesn't seem right label Dec 1, 2021
@sergolius sergolius changed the title NewUpdate not consider nullzero, OmitZero() NewUpdate SET ignores nullzero Dec 1, 2021
@sergolius
Copy link
Author

@vmihailenco
Ok OmitZero is for usecase of Column.
But nullzero?
NewUpdate, Set ignores nullzero. https://bun.uptrace.dev/guide/pg-migration.html#go-zero-values-and-null
It will produce SQL with nil value of type instead of DEFAULT or NULL.
I updated issue with clarified examples.
Copy of SQL sample:

Result SQL with Set:

UPDATE "users" AS "user" SET first_name = '', profile_id = '00000000-0000-0000-0000-000000000000', deleted_at = '0001-01-01 00:00:00+00:00', document_ids = 'null' WHERE (true)

Result SQL with Column:

UPDATE "users" AS "user" SET "first_name" = NULL, "profile_id" = NULL, "deleted_at" = NULL, "document_ids" = NULL WHERE (true)

p.s. It's a challenge to migrate from go-pg. Not because of change of huge codebase, but because of such pitfalls.

vmihailenco added a commit that referenced this issue Dec 1, 2021
fix: respect nullzero when appending struct fields. Fixes #339
@vmihailenco
Copy link
Member

That is a bug / overlook and it should be fixed by #339. Please try master branch.

@sergolius
Copy link
Author

@vmihailenco thanks a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants