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

Singularizing plural table names can generate redeclared models, forcing me to use emit_exact_table_names: true #3835

Open
Jonathan-Landeed opened this issue Feb 11, 2025 · 0 comments
Labels

Comments

@Jonathan-Landeed
Copy link

Jonathan-Landeed commented Feb 11, 2025

Version

1.28.0

What happened?

One of my table names is a plural of another one of my table names. This causes the auto-generated model to be re-declared.

DjangoCeleryBeatPeriodictask redeclared in this block (see details)

type DjangoCeleryBeatPeriodictask struct {
	ID            int32
	Name          string
	Task          string
	Args          string
	Kwargs        string
	Queue         pgtype.Text
	Exchange      pgtype.Text
	RoutingKey    pgtype.Text
	Expires       pgtype.Timestamptz
	Enabled       bool
	LastRunAt     pgtype.Timestamptz
	TotalRunCount int32
	DateChanged   pgtype.Timestamptz
	Description   string
	CrontabID     pgtype.Int4
	IntervalID    pgtype.Int4
	SolarID       pgtype.Int4
	OneOff        bool
	StartTime     pgtype.Timestamptz
	Priority      pgtype.Int4
	Headers       string
	ClockedID     pgtype.Int4
	ExpireSeconds pgtype.Int4
}

type DjangoCeleryBeatPeriodictask struct {
	Ident      int16
	LastUpdate pgtype.Timestamptz
}

I'm generating sqlc models for an existing db, using pg_dump $DATABASE_URL --schema-only > "$OUTPUT_FILE", so I don't have control over the schema. I generally like emit_exact_table_names: false, but this bug means I can't use it for this project. It would be nice if sqlc could detect the collision and use a plural model name for django_celery_beat_periodictasks.

I don't think table struct renaming works because it's based on the generated name, which is the same for both tables. https://docs.sqlc.dev/en/stable/howto/rename.html

Relevant log output

Database schema

--
-- Name: django_celery_beat_periodictask; Type: TABLE; Schema: public; Owner: landeed_user
--

CREATE TABLE public.django_celery_beat_periodictask (
    id integer NOT NULL,
    name character varying(200) NOT NULL,
    task character varying(200) NOT NULL,
    args text NOT NULL,
    kwargs text NOT NULL,
    queue character varying(200),
    exchange character varying(200),
    routing_key character varying(200),
    expires timestamp with time zone,
    enabled boolean NOT NULL,
    last_run_at timestamp with time zone,
    total_run_count integer NOT NULL,
    date_changed timestamp with time zone NOT NULL,
    description text NOT NULL,
    crontab_id integer,
    interval_id integer,
    solar_id integer,
    one_off boolean NOT NULL,
    start_time timestamp with time zone,
    priority integer,
    headers text NOT NULL,
    clocked_id integer,
    expire_seconds integer,
    CONSTRAINT django_celery_beat_periodictask_expire_seconds_check CHECK ((expire_seconds >= 0)),
    CONSTRAINT django_celery_beat_periodictask_priority_check CHECK ((priority >= 0)),
    CONSTRAINT django_celery_beat_periodictask_total_run_count_check CHECK ((total_run_count >= 0))
);

--
-- Name: django_celery_beat_periodictasks; Type: TABLE; Schema: public; Owner: landeed_user
--

CREATE TABLE public.django_celery_beat_periodictasks (
    ident smallint NOT NULL,
    last_update timestamp with time zone NOT NULL
);

SQL queries

Configuration

version: "2"
sql:
  - engine: "postgresql"
    queries: "sqlc/db/query.sql"
    schema: "sqlc/db/schema.sql"
    gen:
      go:
        package: "dbmodels"
        out: "gensqlc/db/project"
        sql_package: "pgx/v5"
        emit_exact_table_names: false
        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

@Jonathan-Landeed Jonathan-Landeed added the bug Something isn't working label Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant