Question: how to properly use UpdateFooParams and Foo class? #691
-
Hey, I just started onboarding with this library, and feel like I'm trying to coerce an antipattern with the Queries.UpdateFoo(ctx, params). The params object and my model object are identical, but I can't cast them. Here's what I have so far: -- name: UpdateUsers :exec
UPDATE users
SET
name = $2,
WHERE id = $1; This generates a type User struct {
ID int32 `json:"id"`
Name string `json:"name"`
} As well as the expected: type UpdateUsersParams struct {
ID int32 `json:"id"`
Name string `json:"name"`
}
func (q *Queries) UpdateUsers(ctx context.Context, arg UpdateUsersParams) error {
_, err := q.exec(ctx, q.updateUsersStmt, updateUsers,
arg.ID,
arg.Name,
)
return err
} The problem here is that I can't pass the user := &db.User{Name: "user1"}
err := q.UpdateUsers(context.Background(), db.UpdateUsersParams{
Name: user.Name,
}) This seems wrong. What am I missing? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
By design, sqlc will never re-use parameter structs for methods, even if those structs have the same fields as other parameter or model structs. Since database schema and queries change often, this design limits the impact of those changes. Taking your example one step further, imagine that we need to add a
type User struct {
ID int32 `json:"id"`
Name string `json:"name"`
CreatedAt time.Time `json:"created_at"`
} Since the column can be null, we don't have to update the |
Beta Was this translation helpful? Give feedback.
-
understood, thank you! Quick side question: Despite passing the |
Beta Was this translation helpful? Give feedback.
-
Correct. The goals is to add migration support further down the line, but it's not there yet |
Beta Was this translation helpful? Give feedback.
By design, sqlc will never re-use parameter structs for methods, even if those structs have the same fields as other parameter or model structs. Since database schema and queries change often, this design limits the impact of those changes.
Taking your example one step further, imagine that we need to add a
created_at
column to theusers
table. This column defaults tonow()
.Since the column can be null, we don't have to updat…