Skip to content

Commit e2a0065

Browse files
authored
docs: add multi-worded table example for renaming (#4067)
1 parent 34afcd4 commit e2a0065

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

docs/howto/rename.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,22 @@ sql:
3030
3131
## Tables
3232
33-
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.
33+
The output structs associated with tables can also be renamed. By default,
34+
the struct name will be the singular version of the table name. For example,
35+
the `authors` table will generate an `Author` struct and the `book_publishers`
36+
table will generate a `BookPublisher` struct.
3437

3538
```sql
3639
CREATE TABLE authors (
3740
id BIGSERIAL PRIMARY KEY,
3841
name text NOT NULL,
3942
bio text
4043
);
44+
45+
CREATE TABLE book_publishers (
46+
id BIGSERIAL PRIMARY KEY,
47+
name text NOT NULL
48+
);
4149
```
4250

4351
```go
@@ -52,9 +60,17 @@ type Author struct {
5260
Name string
5361
Bio sql.NullString
5462
}
63+
64+
type Publisher struct {
65+
ID int64
66+
Name string
67+
}
5568
```
5669

57-
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`).
70+
To rename these structs, you must use the generated struct name. In this
71+
example, that would be `author` and `book_publisher`. Use the `rename` map to
72+
change the name of these struct to `Writer` and `BookPublisher` (note the
73+
camel-casing and the underscore for multi-worded tables).
5874

5975
```yaml
6076
version: '1'
@@ -65,6 +81,7 @@ packages:
6581
queries: query.sql
6682
rename:
6783
author: Writer
84+
book_publisher: Publisher
6885
```
6986

7087
```yaml
@@ -77,6 +94,7 @@ overrides:
7794
go:
7895
rename:
7996
author: Writer
97+
book_publisher: Publisher
8098
```
8199

82100
```go
@@ -91,9 +109,14 @@ type Writer struct {
91109
Name string
92110
Bio sql.NullString
93111
}
112+
113+
type Publisher struct {
114+
ID int64
115+
Name string
116+
}
94117
```
95118

96119
## Limitations
97120

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

0 commit comments

Comments
 (0)