Open
Description
Version
1.28.0
What happened?
Description:
When executing a SQL query that JOINs the same table multiple times with different aliases, SQLC generates Go structs with numbered field suffixes (e.g., Company_2, Company_3) instead of using the specified aliases. This occurs even when using explicit table aliases in the query.
Reproduction Steps:
Create a SQL query with multiple joins to the same table:
-- name: GetProjectDetails :one
SELECT
p.*,
sqlc.embed(m) AS manager_company,
sqlc.embed(c) AS client_company,
sqlc.embed(v) AS vendor_company
FROM projects p
LEFT JOIN companies m ON p.manager_id = m.id -- Manager company
LEFT JOIN companies c ON p.client_id = c.id -- Client company
LEFT JOIN companies v ON p.vendor_id = v.id -- Vendor company
WHERE p.id = $1;
Run sqlc generate
Actual Behavior:
Generated Go struct contains numbered fields:
type GetProjectDetailsRow struct {
ProjectID int32 `json:"project_id"`
// ...
Company Company `json:"company"`
Company_2 Company `json:"company_2"` // Expected: Manager
Company_3 Company `json:"company_3"` // Expected: Client
}
Expected Behavior:
Struct fields should respect SQL aliases:
type GetProjectDetailsRow struct {
ProjectID int32 `json:"project_id"`
// ...
Manager Company `json:"manager"`
Client Company `json:"client"`
Vendor Company `json:"vendor"`
}
Relevant log output
Database schema
SQL queries
Configuration
Playground URL
No response
What operating system are you using?
Linux
What database engines are you using?
PostgreSQL
What type of code are you generating?
Go