Skip to content
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

UUID is not correctly overridden #3272

Closed
alimoli opened this issue Mar 14, 2024 · 3 comments
Closed

UUID is not correctly overridden #3272

alimoli opened this issue Mar 14, 2024 · 3 comments
Labels
bug Something isn't working triage New issues that hasn't been reviewed

Comments

@alimoli
Copy link

alimoli commented Mar 14, 2024

Version

1.25.0

What happened?

uuid is only overridden for the primary keys, but not for the foreign keys.

Relevant log output

type Event struct {
	ID          uuid.UUID
	OrganizerID pgtype.UUID
	Name        string
	Description string
	Status      string
	ImgUrl      string
	Url         string
	CreatedAt   pgtype.Timestamptz
	UpdatedAt   pgtype.Timestamptz
}

type Organizer struct {
	ID        uuid.UUID
	Name      string
	ImgUrl    string
	Url       string
	CreatedAt pgtype.Timestamptz
	UpdatedAt pgtype.Timestamptz
}

Database schema

CREATE TABLE organizer
(
    id         uuid                     DEFAULT gen_random_uuid() PRIMARY KEY,
    name       VARCHAR(100)                           NOT NULL,
    img_url    TEXT                                   NOT NULL,
    url        TEXT                                   NOT NULL,
    created_at TIMESTAMP with time zone DEFAULT now() NOT NULL,
    updated_at TIMESTAMP with time zone DEFAULT now() NOT NULL
);

CREATE TABLE event
(
    id           uuid                     DEFAULT gen_random_uuid() PRIMARY KEY,
    organizer_id uuid REFERENCES organizer (id),
    name         VARCHAR(100)                           NOT NULL,
    description  TEXT                                   NOT NULL,
    status       VARCHAR(100)                           NOT NULL,
    img_url      TEXT                                   NOT NULL,
    url          TEXT                                   NOT NULL,
    created_at   TIMESTAMP with time zone DEFAULT now() NOT NULL,
    updated_at   TIMESTAMP with time zone DEFAULT now() NOT NULL
);

SQL queries

-- name: GetEventById :one
SELECT * FROM event WHERE id = $1;

-- name: GetOrganizerById :many
SELECT * FROM organizer WHERE id = $1;

-- name: ListEventsByOrganizer :many
SELECT * FROM event WHERE organizer_id = $1;

Configuration

version: "2"
sql:
  - engine: "postgresql"
    queries: "db/queries.sql"
    schema: "db/schema.sql"
    gen:
      go:
        package: "repository"
        out: "internal/repository"
        sql_package: "pgx/v5"
        # https://github.com/sqlc-dev/sqlc/issues/3005
        overrides:
          - db_type: "uuid"
            go_type:
              import: "github.com/google/uuid"
              type: "UUID"

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

@alimoli alimoli added bug Something isn't working triage New issues that hasn't been reviewed labels Mar 14, 2024
@alimoli
Copy link
Author

alimoli commented Mar 15, 2024

It is quite blocking, is there anyone that can help?
@kyleconroy @andrewmbenton @victoraugustolls

@andrewmbenton
Copy link
Collaborator

Taking a look and can hopefully have a fix up soon.

@andrewmbenton
Copy link
Collaborator

It's a bit confusing, but the issue is that your foreign key field can be null so you'll need an additional override for nullable uuids.

Note that a single db_type override configuration applies to either nullable or non-nullable columns, but not both. If you want the same Go type to override in both cases, you’ll need to configure two overrides.

Here's a playground link showing how to make it work: https://play.sqlc.dev/p/1f1d7940f4143a4635f7a8afc805416449ed7c7daf65d93b4f39989d64d24976

@andrewmbenton andrewmbenton closed this as not planned Won't fix, can't repro, duplicate, stale Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage New issues that hasn't been reviewed
Projects
None yet
Development

No branches or pull requests

2 participants