Skip to content

SQLC generates duplicate field names with numeric suffixes when joining the same table multiple times #3908

Open
@patriarch11

Description

@patriarch11

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions