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

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

Open
ankisme opened this issue Jun 29, 2023 · 7 comments
Open

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

ankisme opened this issue Jun 29, 2023 · 7 comments
Labels
bug Something isn't working

Comments

@ankisme
Copy link

ankisme commented Jun 29, 2023

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

@ankisme ankisme added bug Something isn't working triage New issues that hasn't been reviewed labels Jun 29, 2023
@ankisme
Copy link
Author

ankisme commented 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      -> 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."

@kyleconroy kyleconroy removed the triage New issues that hasn't been reviewed label Jul 6, 2023
@skabbes
Copy link
Contributor

skabbes commented Jul 16, 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

@ankisme
Copy link
Author

ankisme commented Jul 16, 2023

I am using mysql, not Postgres.

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

@ankisme
Copy link
Author

ankisme commented Jul 16, 2023

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
}

@ankisme
Copy link
Author

ankisme commented Jul 16, 2023

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.

@toqueteos
Copy link

toqueteos commented Jul 25, 2023

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

@ankisme
Copy link
Author

ankisme commented Aug 1, 2023

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
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants