Skip to content

Rename struct property name when using sqlc.embed in multiple joins from same table #3177

Open

Description

@lcnlvrz

Version

1.25.0

What happened?

I just need to perform a query that joins multiple rows from same table and i expect sqlc to take alias from query when build final go structs

In current version when you join multiple times from same table, the final struct looks something like:

struct QueryOutput {
TableRow1  Table
TableRow2 Table
}

Relevant log output

No response

Database schema

CREATE TABLE tracks(
    id SERIAL PRIMARY KEY,
    driver_id INTEGER NOT NULL REFERENCES persons(id),
    recipient_id INTEGER NOT NULL REFERENCES persons(id)
);


CREATE TABLE persons(
    id SERIAL PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    email VARCHAR(255),
    phone VARCHAR(255) NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);

SQL queries

SELECT * FROM tracks
LEFT JOIN persons as driver ON tracks.driver_id = driver.id
LEFT JOIN persons as recipient ON tracks.recipient_id = recipient.id
WHERE id = $1 LIMIT 1;

Configuration

version: '2'
sql:
  - engine: 'postgresql'
    queries: 'query.sql'
    schema: 'migrations'
    gen:
      go:
        package: 'db'
        out: 'db'
        sql_package: 'pgx/v5'
        emit_json_tags: true
        emit_pointers_for_null_types: true
        emit_all_enum_values: true
        overrides:
          - go_struct_tag: 'json:"-"'
            column: '*.password'

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

Activity

  1. added
    bugSomething isn't working
    triageNew issues that hasn't been reviewed
    on Feb 4, 2024
  2. lcnlvrz commented on Feb 4, 2024

    @lcnlvrz
    Author

    By the moment the workaround that works for me is to create views and then use sqlc.embed

    schema.sql

    CREATE VIEW drivers AS (
        SELECT * FROM persons WHERE type = 'driver'
    );
    
    CREATE VIEW recipients AS (
        SELECT * FROM persons WHERE type = 'recipient'
    );

    query.sql

    SELECT sqlc.embed(track), sqlc.embed(driver), sqlc.embed(recipient)
    FROM tracks track
    JOIN drivers driver ON track.driver_id = driver.id
    JOIN recipients recipient ON track.recipient_id = recipient.id
    WHERE track.id = $1 LIMIT 1;
  3. ErikKalkoken commented on Apr 14, 2024

    @ErikKalkoken

    The workaround is great, however it also leads to new types in the response struct, which makes it less useful in some cases.

    e.g. the example would create something like this:

    type GetTrackRow {
    	Track   Track
    	Driver   Driver
    	Recipient Recipient
    }
  4. removed
    triageNew issues that hasn't been reviewed
    on Aug 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions