-
Notifications
You must be signed in to change notification settings - Fork 804
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
UNION creates duplicate fields #1546
Comments
I'm seeing this with SQLite too which generates OK, but then fails with:
|
This is completely broken with SQLite. Input (1.20; using the playground): -- name: GetUnion :many
select 1 union select 2; Generated data structure and query code (comment mine): type GetUnionRow struct {
Column1 int64
Column2 int64
}
func (q *Queries) GetUnion(ctx context.Context) ([]GetUnionRow, error) {
rows, err := q.db.QueryContext(ctx, getUnion)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetUnionRow
for rows.Next() {
var i GetUnionRow
if err := rows.Scan(&i.Column1, &i.Column2); err != nil {
// ^^^^ ^^^^^^^^^ ^^^^^^^^^
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
} Meanwhile in
I'd expect the generated code to be closer to this: type GetUnionRow struct {
Column1 int64
}
func (q *Queries) GetUnion(ctx context.Context) ([]GetUnionRow, error) {
rows, err := q.db.QueryContext(ctx, getUnion)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetUnionRow
for rows.Next() {
var i GetUnionRow
if err := rows.Scan(&i.Column1); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
} Or better, produce an |
I've just upgraded to v1.21.0 and that appears to have resolved the issue, including generating much more reasonable structs 👏 type RetrievePackageAdvisoriesRow struct {
- Platform string
- Organisation string
- Repo string
- PackageName string
- Version sql.NullString
- CurrentVersion sql.NullString
- DepTypes string
- AdvisoryType string
- Description string
- Platform_2 string
- Organisation_2 string
- Repo_2 string
- PackageName_2 string
- Version_2 string
- CurrentVersion_2 sql.NullString
- DepTypes_2 string
- AdvisoryType_2 string
- Description_2 string
- Platform_3 string
- Organisation_3 string
- Repo_3 string
- PackageName_3 string
- Version_3 string
- CurrentVersion_3 sql.NullString
- DepTypes_3 string
- AdvisoryType_3 interface{}
- Description_3 interface{}
+ Platform string
+ Organisation string
+ Repo string
+ PackageName string
+ Version sql.NullString
+ CurrentVersion sql.NullString
+ DepTypes string
+ AdvisoryType string
+ Description string
} |
Version
1.12.0
What happened?
Generated code:
Similar to #568. It always creates duplicate fields with
_2
suffix. UsingDISTINCT
also does not work, checked the #896, it's not supported anyway.Relevant log output
No response
Database schema
SQL queries
Configuration
Playground URL
No response
What operating system are you using?
macOS
What database engines are you using?
PostgreSQL
What type of code are you generating?
Go
The text was updated successfully, but these errors were encountered: