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

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

Open
jmillerv opened this issue Oct 12, 2022 · 3 comments
Open

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

jmillerv opened this issue Oct 12, 2022 · 3 comments

Comments

@jmillerv
Copy link

jmillerv commented Oct 12, 2022

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:

  • meta -> Meturm
  • media -> Medium
  • curves -> Curf
  • data -> Datum

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 jmillerv added bug Something isn't working triage New issues that hasn't been reviewed labels Oct 12, 2022
@jmillerv
Copy link
Author

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 kyleconroy added 📚 postgresql 💻 linux 🔧 golang and removed triage New issues that hasn't been reviewed labels Nov 9, 2022
@kyleconroy
Copy link
Collaborator

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
Copy link
Collaborator

kyleconroy commented Oct 9, 2023

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.

@kyleconroy kyleconroy changed the title Singularizing "Meta" Error Unexpected struct name when a table name ends with 'meta' Oct 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants