Skip to content

Sqlc will auto convent BookID to Bookid. It's a bug. #2392

Description

@ankisme

Version

1.18.0

What happened?

My sql field is BookID, but sqlc will convert BookID to Bookid.

CREATE TABLE authors (
  id   BIGINT  NOT NULL AUTO_INCREMENT PRIMARY KEY,
  name text    NOT NULL,
  bio  text,
  BookID int
);
type Author struct {
	ID     int64
	Name   string
	Bio    sql.NullString
	Bookid sql.NullInt32
}

Maybe many people think that it is not a bug.

But I really think it is a bug.

I like BookID or BookId, and I don't like Bookid at all.

ThisIsAnExample is much more better than Thisisanexample.

AuthorName is much more better than Authorname.

And I could not found any configuration to forbid the auto rename.

Relevant log output

No response

Database schema

No response

SQL queries

No response

Configuration

No response

Playground URL

No response

What operating system are you using?

No response

What database engines are you using?

No response

What type of code are you generating?

No response

Activity

  1. added
    bugSomething isn't working
    triageNew issues that hasn't been reviewed
    on Jun 29, 2023
  2. ankisme commented on Jun 29, 2023

    @ankisme
    Author

    OK, I see the documentation in https://docs.sqlc.dev/en/stable/reference/config.html#renaming-fields

    Now I know the algorithm is this.

    Struct field names are generated from column names using a simple algorithm: split the column name on underscores and capitalize the first letter of each part.
    
    account     -> Account
    spotify_url -> SpotifyUrl
    app_id      -> AppID
    

    I will say it is really not a good idea.

    Because in the real world, not all the table field in database is like aa_bb_cc.

    There are so many so many so many table field is like AaBbCc.

    Maybe someone will say "It's not a bug. It's a feature."

  3. removed
    triageNew issues that hasn't been reviewed
    on Jul 6, 2023
  4. skabbes commented on Jul 16, 2023

    @skabbes
    Contributor

    In Postgres, table names are normalized to lowercase unless you double quote those columns:

    So for example

    CREATE TABLE authors (
      -- ...
      BookID int
    );

    Is functionally the same as:

    CREATE TABLE authors (
      -- ...
      bookid int
    );

    However, if you specify your column with double quotes, you will see that sqlc in fact generates the correct struct name (see my sqlc play link):

    CREATE TABLE authors (
      id   BIGINT  NOT NULL AUTO_INCREMENT PRIMARY KEY,
      name text    NOT NULL,
      bio  text,
      "BookID" int
    );

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

  5. ankisme commented on Jul 16, 2023

    @ankisme
    Author

    I am using mysql, not Postgres.

    And it will have sql error if I use "BookID" int in mysql

  6. ankisme commented on Jul 16, 2023

    @ankisme
    Author

    I also try `BookID` int

    CREATE TABLE authors1 (
      id   BIGINT  NOT NULL AUTO_INCREMENT PRIMARY KEY,
      name text    NOT NULL,
      bio  text,
      `BookID` int
    )

    and sqlc generate

    type Author struct {
    	ID     int64
    	Name   string
    	Bio    sql.NullString
    	Bookid sql.NullInt32
    }
  7. ankisme commented on Jul 16, 2023

    @ankisme
    Author

    Hope to add the feature to keep the case of mysql field.

    The reason why I need to keep the case of mysql field, is that I use many auto generated golang code generated by tool.

  8. toqueteos commented on Jul 25, 2023

    @toqueteos

    OK, I see the documentation in https://docs.sqlc.dev/en/stable/reference/config.html#renaming-fields

    [..]

    @ankisme The section below that explains how to overwrite field names, have you tried that?

    Working example: https://play.sqlc.dev/p/2f4eef0c6c87569b5a1b531e6a4a37b0849d9d0d26959ec6a89dcfd982c9f47c

    image

  9. ankisme commented on Aug 1, 2023

    @ankisme
    Author

    OK, I see the documentation in https://docs.sqlc.dev/en/stable/reference/config.html#renaming-fields
    [..]

    @ankisme The section below that explains how to overwrite field names, have you tried that?

    Working example: https://play.sqlc.dev/p/2f4eef0c6c87569b5a1b531e6a4a37b0849d9d0d26959ec6a89dcfd982c9f47c

    image

    I have not try. Because I fork sqlc and modify some code to support original field name like AaBbCc.

    Maybe I will try Renaming fields later, but it't not convient, because I should generate all lowercase fieldname to original fieldname, and save them into .json.

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