Giter Site home page Giter Site logo

Comments (1)

bevzzz avatar bevzzz commented on August 18, 2024

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.:

bun/schema/table.go

Lines 378 to 381 in 8a43835

if s, ok := tag.Option("default"); ok {
field.SQLDefault = s
field.NullZero = true
}

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:

bun/query_insert.go

Lines 335 to 338 in 8a43835

case (f.IsPtr && f.HasNilValue(strct)) || (f.NullZero && f.HasZeroValue(strct)):
if q.db.features.Has(feature.DefaultPlaceholder) {
b = append(b, "DEFAULT"...)
} else if f.SQLDefault != "" {

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:

bun/schema/field.go

Lines 97 to 99 in 8a43835

if (f.IsPtr && fv.IsNil()) || (f.NullZero && f.IsZero(fv)) {
return dialect.AppendNull(b)
}

I think adding a HasDefault bool field to schema.Field and using it checking for SQLDefault != "" in the InsertQuery instead would be a good long-term solution.

from bun.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.