Open vichu opened 11 months ago
tl;dr: Try using .Value()
method to control which value gets appended to the query.
The following should work:
db.NewUpdate().
Model(entity).
Column("is_active").
Value("is_active", "false"). // will substitute to is_active = false
WherePK().
Exec(ctx)
This is a workaround, because the behaviour you've described looks buggy. For more about that, read on.
This happens because any field that has a bun:"default:x"
tag also gets a "nullzero"
tag.:
Why? Because for INSERT
queries bun
needs to be able to correctly decide to append DEFAULT
placeholder for a field that has a zero value:
That's helpful, even though not all dialects support this feature (looking at you, SQLite).
But then, once we try to set a field that has a default value to false
(zero boolean value) in UpdateQuery
we get the bug you've described. It happens here:
https://github.com/uptrace/bun/blob/8a4383505d7e954897b23811a412b9cdafaf41eb/schema/field.go#L97-L99
I think adding a checking for HasDefault bool
field to schema.Field
and using itSQLDefault != ""
in the InsertQuery
instead would be a good long-term solution.
Hello,
I'm currently using bun in a project and I've encountered an issue with boolean fields defaulting to NULL instead of false during updates.
Here's the bun model struct I'm using:
And here's the function I'm using to update the Entity:
In my test case, when I'm setting
IsActive
tofalse
explicitly and try to update the record, I get an error: null value in column "is_active" violates not-null constraint error, which indicates that the is_active column is being set to NULL.I've checked/debugged my code and can confirm that
IsActive
is being set to false and not NULL.This issue seems to occur only during updates when the boolean value is explicitly set to
false
.Thank you.