Skip to content

docs: add multi-worded table example for renaming #4067

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions docs/howto/rename.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,22 @@ sql:

## Tables

The output structs associated with tables can also be renamed. By default, the struct name will be the singular version of the table name. For example, the `authors` table will generate an `Author` struct.
The output structs associated with tables can also be renamed. By default,
the struct name will be the singular version of the table name. For example,
the `authors` table will generate an `Author` struct and the `book_publishers`
table will generate a `BookPublisher` struct.

```sql
CREATE TABLE authors (
id BIGSERIAL PRIMARY KEY,
name text NOT NULL,
bio text
);

CREATE TABLE book_publishers (
id BIGSERIAL PRIMARY KEY,
name text NOT NULL
);
```

```go
Expand All @@ -52,9 +60,17 @@ type Author struct {
Name string
Bio sql.NullString
}

type Publisher struct {
ID int64
Name string
}
```

To rename this struct, you must use the generated struct name. In this example, that would be `author`. Use the `rename` map to change the name of this struct to `Writer` (note the uppercase `W`).
To rename these structs, you must use the generated struct name. In this
example, that would be `author` and `book_publisher`. Use the `rename` map to
change the name of these struct to `Writer` and `BookPublisher` (note the
camel-casing and the underscore for multi-worded tables).

```yaml
version: '1'
Expand All @@ -65,6 +81,7 @@ packages:
queries: query.sql
rename:
author: Writer
book_publisher: Publisher
```

```yaml
Expand All @@ -77,6 +94,7 @@ overrides:
go:
rename:
author: Writer
book_publisher: Publisher
```

```go
Expand All @@ -91,9 +109,14 @@ type Writer struct {
Name string
Bio sql.NullString
}

type Publisher struct {
ID int64
Name string
}
```

## Limitations

Rename mappings apply to an entire package. Therefore, a column named `foo` and
a table name `foo` can't map to different rename values.
a table name `foo` can't map to different rename values.
Loading