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

Update().SetBlacklist() doesn't understand embedded structs #26

Closed
braunsquared opened this issue Oct 15, 2015 · 3 comments
Closed

Update().SetBlacklist() doesn't understand embedded structs #26

braunsquared opened this issue Oct 15, 2015 · 3 comments

Comments

@braunsquared
Copy link

When using Update(...).SetBlacklist(...), the reflection methods fail to traverse into embedded structs with the error message:

<xxx Value> must have db struct tags for all fields: db:""

It seems there is a use of reflectx throughout the dat package which understands struct traversal, but the update methods use their own reflection.

For example:

type Model struct {
    ID        string    `json:"id" db:"id"`
    CreatedAt time.Time `json:"createdAt" db:"created_at"`
    UpdatedAt time.Time `json:"updatedAt" db:"updated_at"`
}

type Customer struct {
    Model
    First              string         `json:"first" db:"first"`
    Last               string         `json:"last" db:"last"`
}

customer := Customer{}
err := tx.
    Update(TableCustomers).
    SetBlacklist(customer, "id", "created_at", "updated_at").
    Where("id = $1", customer.ID).
    Returning("updated_at").
    QueryScalar(&customer.UpdatedAt)

Not only that, but it fails with a panic which takes everything down with it.

@steveoc64
Copy link

Just got stung on this one - was assuming the error was in my code. Was a bit hard to trace it back to DAT itself.

Fixing this (inside dat) looks to be a little bit of work, but nothing too radical.

Using this pattern as a workaround in the meantime to get my code working :

DB.update("table").
  Set("column_name1", record.Field1).
  Set("column_name2", record.Field2).
  Set("column_name2", record.Field3.SomeStructure.SomeOtherStructure.SomeField).
...

Its ugly compared to using Black/White list, but at least its explicit, and gets me working again.

Looking forward to seeing a fix on this one.

@mgutz mgutz closed this as completed in cd3fd99 Dec 21, 2015
@mgutz
Copy link
Owner

mgutz commented Dec 21, 2015

I apologize, been really busy. Thank you for pointing out I was doing custom reflection in Update.

@levicook
Copy link

levicook commented Jan 1, 2016

@steveoc64 thanks for posting that workaround! Took me a while to trace through what was happening too. Wasn't sure what to do when I got to the bottom.

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

4 participants