Skip to content

Commit 9fe9e2d

Browse files
committed
keep plural when not matching table name
1 parent 3250847 commit 9fe9e2d

File tree

3 files changed

+14
-17
lines changed

3 files changed

+14
-17
lines changed

internal/codegen/golang/result.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ type goEmbed struct {
122122

123123
// look through all the structs and attempt to find a matching one to embed
124124
// We need the name of the struct and its field names.
125-
func newGoEmbed(req *plugin.CodeGenRequest, tableName string, embed *plugin.Identifier, structs []Struct, defaultSchema string) *goEmbed {
125+
func newGoEmbed(columnName string, embed *plugin.Identifier, structs []Struct, defaultSchema string) *goEmbed {
126126
if embed == nil {
127127
return nil
128128
}
@@ -141,12 +141,9 @@ func newGoEmbed(req *plugin.CodeGenRequest, tableName string, embed *plugin.Iden
141141
fields := make([]Field, len(s.Fields))
142142
copy(fields, s.Fields)
143143

144-
structName := tableName
145-
if !req.Settings.Go.EmitExactTableNames {
146-
structName = inflection.Singular(inflection.SingularParams{
147-
Name: structName,
148-
Exclusions: req.Settings.Go.InflectionExcludeTableNames,
149-
})
144+
structName := s.Name
145+
if s.Table.Name != columnName {
146+
structName = columnName
150147
}
151148

152149
return &goEmbed{
@@ -294,7 +291,7 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
294291
columns = append(columns, goColumn{
295292
id: i,
296293
Column: c,
297-
embed: newGoEmbed(req, c.Name, c.EmbedTable, structs, req.Catalog.DefaultSchema),
294+
embed: newGoEmbed(c.Name, c.EmbedTable, structs, req.Catalog.DefaultSchema),
298295
})
299296
}
300297
var err error

internal/endtoend/testdata/sqlc_embed/mysql/go/query.sql.go

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/sqlc_embed/mysql/query.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ INNER JOIN baz.users bu ON users.id = bu.id;
5151
-- name: ListUserLink :many
5252
SELECT
5353
sqlc.embed(owner),
54-
sqlc.embed(consumer)
54+
sqlc.embed(consumers)
5555
FROM
5656
user_links
5757
INNER JOIN users AS owner ON owner.id = user_links.owner_id
58-
INNER JOIN users AS consumer ON consumer.id = user_links.consumer_id;
58+
INNER JOIN users AS consumers ON consumers.id = user_links.consumer_id;

0 commit comments

Comments
 (0)