Skip to content

Commit

Permalink
feat: add support for column:name to specify column name
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Nov 29, 2021
1 parent 5ddb4b4 commit e37b460
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions schema/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ func (t *Table) processBaseModelField(f reflect.StructField) {

if isKnownTableOption(tag.Name) {
internal.Warn.Printf(
"%s.%s tag name %q is also an option name; is it a mistake?",
t.TypeName, f.Name, tag.Name,
"%s.%s tag name %q is also an option name, is it a mistake? Try table:%s.",
t.TypeName, f.Name, tag.Name, tag.Name,
)
}

Expand Down Expand Up @@ -316,19 +316,23 @@ func (t *Table) processBaseModelField(f reflect.StructField) {

//nolint
func (t *Table) newField(f reflect.StructField, index []int) *Field {
sqlName := internal.Underscore(f.Name)
tag := tagparser.Parse(f.Tag.Get("bun"))

sqlName := internal.Underscore(f.Name)
if tag.Name != "" && tag.Name != sqlName {
if isKnownFieldOption(tag.Name) {
internal.Warn.Printf(
"%s.%s tag name %q is also an option name; is it a mistake?",
t.TypeName, f.Name, tag.Name,
"%s.%s tag name %q is also an option name, is it a mistake? Try column:%s.",
t.TypeName, f.Name, tag.Name, tag.Name,
)
}
sqlName = tag.Name
}

if s, ok := tag.Option("column"); ok {
sqlName = s
}

for name := range tag.Options {
if !isKnownFieldOption(name) {
internal.Warn.Printf("%s.%s has unknown tag option: %q", t.TypeName, f.Name, name)
Expand Down Expand Up @@ -866,7 +870,8 @@ func isKnownTableOption(name string) bool {

func isKnownFieldOption(name string) bool {
switch name {
case "alias",
case "column",
"alias",
"type",
"array",
"hstore",
Expand Down

0 comments on commit e37b460

Please sign in to comment.