Files
go-admin/template/v4/model.go.template
T
zhangwenjian d30b9c103f fix🐛: import "time" in a generated model only for fields it declares
The model template imported "time" for any queryable time.Time column,
though whether a column is queryable only shapes the DTO. A queryable
created_at, which the model takes from models.ModelTime rather than
declaring, left the import unused and the generated package failed to
compile.
2026-09-27 16:07:09 +08:00

68 lines
2.2 KiB
Plaintext

package models
import (
{{- /*
The model declares a time.Time field for every such column except the
three models.ModelTime supplies. Whether a column is queryable is the
DTO's business, not the model's: counting it here imported "time" for a
queryable created_at that the model never declares.
*/ -}}
{{- $bb := false -}}
{{- range .Columns -}}
{{- if eq .GoField "CreatedAt" -}}
{{- else if eq .GoField "UpdatedAt" -}}
{{- else if eq .GoField "DeletedAt" -}}
{{- else -}}
{{- if eq .GoType "time.Time" -}}{{- $bb = true -}}{{- end -}}
{{- end -}}
{{- end -}}
{{- if eq $bb true }}
"time"
{{- end }}
"go-admin/common/models"
)
{{/*
models.Model supplies an int primary key named Id, so it stands in for the
key column only when that is exactly the key the table has. Any other key -
a different name, or a string - is declared as its own field, or GetId below
would name a field that does not exist.
*/ -}}
{{- $embed := false -}}
{{- range .Columns -}}{{- if and .Pk (eq .GoField "Id") (eq .GoType "int") -}}{{- $embed = true -}}{{- end -}}{{- end -}}
type {{.ClassName}} struct {
{{- if $embed }}
models.Model
{{- end }}
{{ range .Columns -}}
{{$x := .Pk}}
{{- if ($x) }}
{{- if not $embed }}
{{.GoField}} {{.GoType}} `json:"{{.JsonField}}" gorm:"primaryKey;type:{{.ColumnType}};comment:{{- if eq .ColumnComment "" -}}{{.GoField}}{{- else -}}{{.ColumnComment}}{{end -}}"`
{{- end }}
{{- else if eq .GoField "CreatedAt" -}}
{{- else if eq .GoField "UpdatedAt" -}}
{{- else if eq .GoField "DeletedAt" -}}
{{- else if eq .GoField "CreateBy" -}}
{{- else if eq .GoField "UpdateBy" -}}
{{- else }}
{{.GoField}} {{.GoType}} `json:"{{.JsonField}}" gorm:"type:{{.ColumnType}};comment:{{- if eq .ColumnComment "" -}}{{.GoField}}{{- else -}}{{.ColumnComment}}{{end -}}"` {{end -}}
{{- end }}
models.ModelTime
models.ControlBy
}
func ({{.ClassName}}) TableName() string {
return "{{.TBName}}"
}
func (e *{{.ClassName}}) Generate() models.ActiveRecord {
o := *e
return &o
}
func (e *{{.ClassName}}) GetId() interface{} {
return e.{{.PkGoField}}
}