Skip to content

fix(insert): keep field in VALUES when any slice element has a non-default value - #1412

Open
JoeJoeflyn wants to merge 1 commit into
uptrace:masterfrom
JoeJoeflyn:fix/slice-insert-dropped-fields-1394
Open

fix(insert): keep field in VALUES when any slice element has a non-default value#1412
JoeJoeflyn wants to merge 1 commit into
uptrace:masterfrom
JoeJoeflyn:fix/slice-insert-dropped-fields-1394

Conversation

@JoeJoeflyn

Copy link
Copy Markdown

Fixes #1394

getFields() inspected only the first slice element to decide whether a NotNull + nullzero/default: field should move to RETURNING instead of VALUES. If a later element had a real value, it was silently dropped and replaced by the column DEFAULT.

Now the field moves to RETURNING only when every element marshals to DEFAULT/NULL. Single-struct path unchanged.

-- Before: email dropped, second row's value lost
INSERT INTO t (name) VALUES ('zero-email'), ('with-email') RETURNING id, email
-- After:  email kept, both values persisted
INSERT INTO t (name, email) VALUES ('zero-email', 'none'), ('with-email', 'alice@example.com') RETURNING id, email

Tested on PostgreSQL (pg + pgx) and SQLite with -race. New regression test TestIssue1394_DroppedFieldValueOnSliceInsert included.

…fault value

When inserting a slice of structs, getFields() inspected only the first
element to decide whether a NotNull field with nullzero/default should
be moved to the RETURNING clause instead of the VALUES clause. If a
later element carried a real value for such a field, that value was
silently dropped from the INSERT and replaced by the column DEFAULT.

A field is now moved to RETURNING only when it marshals to DEFAULT/NULL
in every element of the slice. If any element has a real value, the
field stays in the INSERT column list so the value reaches the database.

Fixes uptrace#1394
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dropped field value(s) on slice model insert

1 participant