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

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

Closed
1 of 3 tasks
ocomsoft opened this issue Aug 2, 2021 · 1 comment
Closed
1 of 3 tasks

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

ocomsoft opened this issue Aug 2, 2021 · 1 comment

Comments

@ocomsoft
Copy link

ocomsoft commented Aug 2, 2021

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

@doug-martin
Copy link
Owner

@ocomsoft thank you for submitting an issue with a reproducible example! I just released a fix for this in v9.15.1

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

No branches or pull requests

2 participants