sqlc-dev / sqlc

Generate type-safe code from SQL
https://sqlc.dev
MIT License
13.45k stars 804 forks source link

Unexpected struct name when a table name ends with 'meta' #1892

Open jmillerv opened 2 years ago

jmillerv commented 2 years ago

Version

1.15.0

What happened?

I have a table called items_meta. When sqlc goes to make the struct the logic that attempts to make idiomatic go translates it to type TableItemsMetum struct. While this is usable that isn't, according to any reading I can find, an appropriate way to "singularize" that. Is sqlc applying the rules for data/datum to words that end in a?

This behavior also happens with the following words:

Database schema

CREATE TABLE bazaar.items_meta (
                                   id uuid NOT NULL DEFAULT uuid_generate_v4(), -- primary key
                                   items_id uuid NOT NULL, -- foreign key to items.id
                                   location_found varchar NULL,
                                   identified bool NULL DEFAULT false,
                                   created_date date NULL DEFAULT now(),
                                   updated_date date NULL DEFAULT now(),
                                   notes varchar NULL, -- additional information about the item
                                   invested bool NULL DEFAULT false,
                                   CONSTRAINT items_meta_pk PRIMARY KEY (id)
);

SQL queries

No response

Configuration

version: 2
sql:
  - engine: "postgresql"
    schema: "schema.sql"
    queries: "query.sql"
    gen:
      go:
        package: "psql"
        out: "psql"
        emit_json_tags: true

Playground URL

https://play.sqlc.dev/p/66ae898df20d0347156df3f8bf842c3ed04709d5a9da5acf3aec9a229a2941db

What operating system are you using?

Linux

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

jmillerv commented 2 years ago

This is not an urgent issue as it doesn't hinder progress, I just thought it was a bit bizarre and wanted to flag it.

kyleconroy commented 2 years ago

Also an issues with media

This one isn't a huge issue, I just have to remember to use the right name and to switch back when it's fixed. So in my migration schema, I have a couple structs that have media in them:

CREATE TABLE "name_media" (
  "id" smallserial PRIMARY KEY
  // other fields.
);

CREATE TABLE "another_name_media" (
  "id" bigserial PRIMARY KEY
  // other fields.
);

These are the generated structs:
type NameMedium struct {
    ID         int16  `json:"id"`
    // other fields.
}

type AnotherNameMedium struct {
    ID         int64     `json:"id"`
    // other fields.
}

I 'made' a playground for this, but realistically all I did was change authors to name_media and it happens. https://play.sqlc.dev/p/fe32fa791d8da82e238e014619ebfa9fefbfa3fa2bceaa363c2f40fdbba55b9b

kyleconroy commented 1 year ago

You can actually fix this, but you have to know which key to use in the rename configuration. For bazaar.items_meta the key is `bazaar_item_metum".

{
  "version": "1",
  "rename": {
    "bazaar_items_metum": "BazaarItemsMeta",
  },
  "packages": [
    {
      "path": "db",
      "engine": "postgresql",
      "schema": "query.sql",
      "queries": "query.sql"
    }
  ]
}

https://play.sqlc.dev/p/c955263efe1a2f6b8ca8e1d040032caeccb181aaaad184d81907d3e66e9a72c2

You can also set the emit_exact_table_names option to true, but that applies to all tables.