Skip to content

Using Update().Set(item) and the values are wrong #290

Closed
@ocomsoft

Description

I am loving this module. I have found a bug, I am hoping for you this would be easy to see what's gone wrong.

Describe the bug
When using a Struct to do an Update, the value for other fields are used instead of the right field.

To Reproduce

package test

import (
	"fmt"
	"github.com/doug-martin/goqu/v9"
	"github.com/guregu/null"
	"testing"
	"time"
)

func TestStructToRecordFail(t *testing.T) {
	type OcomModel struct {
		ID           uint      `json:"id" db:"id" goqu:"skipinsert"`
		CreatedDate  time.Time `json:"created_date" db:"created_date" goqu:"skipupdate"`
		ModifiedDate time.Time `json:"modified_date" db:"modified_date"`
	}

	type ActiveModel struct {
		OcomModel
		ActiveStartDate null.Time `json:"active_start_date" db:"active_start_date"`
		ActiveEndDate   null.Time `json:"active_end_date" db:"active_end_date"`
	}

	type CodeModel struct {
		ActiveModel

		Code        string `json:"code" db:"code"`
		Description string `json:"description" binding:"required" db:"description"`
	}

	type CodeExample struct {
		CodeModel
	}

	var item CodeExample
	item.Code = "Code"
	item.Description = "Description"
	item.ID = 1  // Value set HERE!
	item.CreatedDate = time.Date(
		2021, 1, 1, 1, 1, 1, 1, time.UTC)
	item.ModifiedDate =  time.Date(
		2021, 2, 2, 2, 2, 2, 2, time.UTC) // The Value we Get!
	item.ActiveStartDate = null.NewTime(time.Date(
		2021, 3, 3, 3, 3, 3, 3, time.UTC), true)

	update_query :=  goqu.From("example").Update().Set(item).Where(goqu.C("id").Eq(1))

	sql, params, err := update_query.ToSQL() // UPDATE "example" SET "active_end_date"=NULL,"active_start_date"='2021-03-03T03:03:03.000000003Z',"code"='Code',"created_date"='2021-02-02T02:02:02.000000002Z',"description"='Description',"id"='2021-02-02T02:02:02.000000002Z',"modified_date"='2021-02-02T02:02:02.000000002Z' WHERE ("id" = 1) [] <nil>

	fmt.Println(sql, params, err)

	if sql == "UPDATE \"example\" SET \"active_end_date\"=NULL,\"active_start_date\"='2021-03-03T03:03:03.000000003Z',\"code\"='Code',\"created_date\"='2021-02-02T02:02:02.000000002Z',\"description\"='Description',\"id\"='2021-02-02T02:02:02.000000002Z',\"modified_date\"='2021-02-02T02:02:02.000000002Z' WHERE (\"id\" = 1)" {
		t.Fail()
	}
}

Expected behavior
Notice the SQL generated has
"id"='2021-02-02T02:02:02.000000002Z'
I would expect it to be
"id" = 1

Dialect:

  • postgres
  • mysql
  • sqlite3

Additional context
I tried to debug this but I could not understand what was happening.

Also this MAY BE related but I had a similar error when I had a field called

DeletedAt    gorm.DeletedAt 

In the OcomModel struct.

I am not sure this is a bug to report as I am using a field with what would probably be an unsupported type.

I thought it was the gorm type so I removed it and it seemed to work for insert and select

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions