Repository navigation
Sqlc will auto convent BookID to Bookid. It's a bug. #2392
Description
Activity
- addedbugSomething isn't workingSomething isn't workingtriageNew issues that hasn't been reviewedNew issues that hasn't been reviewed
on Jun 29, 2023 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 -> AppIDI 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."
- removedtriageNew issues that hasn't been reviewedNew issues that hasn't been reviewed
on Jul 6, 2023 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
I am using mysql, not Postgres.
And it will have sql error if I use
"BookID" intin mysqlI also try
`BookID` intCREATE 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 }
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.
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
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
I have not try. Because I fork sqlc and modify some code to support original field name like AaBbCc.
Maybe I will try
Renamingfields later, but it't not convient, because I should generate all lowercase fieldname to original fieldname, and save them into .json.


Version
1.18.0
What happened?
My sql field is BookID, but sqlc will convert BookID to Bookid.
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