Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Use model struct as method parameter#875

Unanswered
maratori asked this question inFeature Requests & Ideas
Jan 31, 2021· 4 comments· 3 replies
Discussion options

Some of insert queries contains all columns of table as input parameters.
In that case generated method may apply model struct instead of...Params struct.

For example following method
https://github.com/kyleconroy/sqlc/blob/bda1a0089e042a33f64f9dd1b59a2c3631fb659e/examples/ondeck/postgresql/city.sql.go#L28
may easily become

func (q*Queries)CreateCity(ctx context.Context,argCity) (City,error) {

Because table contains two columns
https://github.com/kyleconroy/sqlc/blob/bda1a0089e042a33f64f9dd1b59a2c3631fb659e/examples/ondeck/postgresql/schema/0001_city.sql#L1-L4
and the query uses all of them
https://github.com/kyleconroy/sqlc/blob/bda1a0089e042a33f64f9dd1b59a2c3631fb659e/examples/ondeck/postgresql/query/city.sql#L15-L21

Implementation

For output parameters there is struct "deduplication"
https://github.com/kyleconroy/sqlc/blob/bda1a0089e042a33f64f9dd1b59a2c3631fb659e/internal/codegen/golang/result.go#L183

I believe we need to introduce the same deduplication for input parameters
https://github.com/kyleconroy/sqlc/blob/bda1a0089e042a33f64f9dd1b59a2c3631fb659e/internal/codegen/golang/result.go#L166

You must be logged in to vote

Replies: 4 comments 3 replies

Comment options

Hi. Has anything become of this idea?

You must be logged in to vote
3 replies
@kyleconroy
Comment options

Nothing has been decided. If we do add support for this, it will need to go behind a configuration flag so that we don't break existing applications.

@Crocmagnon
Comment options

I would love to see this implemented. Sometimes it's also easier to pass the full entity around instead of having to create another for the occasion of executing a DB query, even if the query doesn't use all fields of the entity.

@omertoast
Comment options

This can make creating queries for seeding the database a lot easier, otherwise, you'll end up with twoCreate queries with duplicated insert fields. This is not a problem if you have few columns but when you have 50 columns in a table, you need to keep track of both queries and update both when you add a new column to the table.

I'd like to know if there is any way right now to do this nicely.

Comment options

Seems like a flag inquery.sql, to note that you'd like this function to persist or update a whole entity rather than using a params struct, would be really useful. something akin to

-- name: PersistNewCity :entity :oneINSERT INTO city (* )VALUES (** )RETURNING*;

results in

func (q*Queries)PersistNewCity(ctx context.Context,argCity) (City,error) {

where expands** (or whatever) into all columns except the primary key.

You must be logged in to vote
0 replies
Comment options

+1

How do maintainers feel about it? We can probably help open a PR

You must be logged in to vote
0 replies
Comment options

We really need it

You must be logged in to vote
0 replies
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Labels
None yet
8 participants
@maratori@ryandotsmith@kyleconroy@Crocmagnon@tloriato@ciricc@ecsdavidt@omertoast
Converted from issue

This discussion was converted from issue #872 on February 02, 2021 06:38.


[8]ページ先頭

©2009-2025 Movatter.jp