You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am developer transitioning from Node.js to go and it has been smooth. But, I'm finding it hard to update a model without overwriting the role in the db with the null data. Example:
I've a User Struct:
// User represents a user's record
type User struct {
ID int `json:"id" db:"pk,id"`
Firstname string `json:"firstname" db:"firstname"`
Lastname string `json:"lastname" db:"lastname"`
Email string `json:"email" db:"email"`
IsDeleted bool `json:"isDeleted" db:"isDeleted"`
Password string `json:"-" db:"password"`
Country string `json:"country" db:"country"`
State string `json:"state" db:"state"`
FacebookToken *string `json:"fbToken" db:"fbToken"`
TwitterToken *string `json:"twitterToken" db:"twitterToken"`
CreatedAt time.Time `json:"createdAt" db:"createdAt"`
UpdatedAt time.Time `json:"updatedAt" db:"updatedAt"`
}
// Validate helps with validating the user field
func (u User) Validate() error {
return validation.ValidateStruct(&u,
validation.Field(&u.Firstname, validation.Required, validation.Length(0, 120)),
validation.Field(&u.Lastname, validation.Required, validation.Length(0, 120)),
validation.Field(&u.Password, validation.Required, validation.Length(8, 120), is.Alphanumeric),
validation.Field(&u.Email, validation.Required, is.Email),
validation.Field(&u.Country, validation.Required),
validation.Field(&u.State, validation.Required),
)
}
// IsPassValid - Verify if password is valid
func (u User) IsPassValid(pass string) error {
return bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(pass))
}
// GenerateHash - Helper function to help generate password hash. It returns error if hasing fails, and user if successful.
func (u *User) GenerateHash() error {
bytes, err := bcrypt.GenerateFromPassword([]byte(u.Password), 14)
u.Password = string(bytes)
return err
}
// TableName - Utilized by ozzo-dbx for referencing main table name
func (u User) TableName() string {
return "users"
}
The update works as a crud operation so it updates all the data available in the object. To update only specific fields you would have to define them as described in https://github.com/go-ozzo/ozzo-dbx/blob/master/README.md#building-data-manipulation-queries .
Alternatively load the data into a struct, update the fields there and write it all back using a common update ()
Hi.
I am developer transitioning from
Node.js
togo
and it has been smooth. But, I'm finding it hard to update a model without overwriting the role in the db with the null data. Example:I've a User Struct:
So, to create a user, I did:
And this gives me:
But, whenever I try to update just a specific field, such as in this case:
The data I didn't add are overwritten. How can I update without hardcoding which fields needs to be updated.
This is what I had to do to fix that. If you think there is a better way, please, do let me know:
What would you suggest I do to get this done. Thanks
The text was updated successfully, but these errors were encountered: