Files
go-admin/template/v4/model.go.template
T
zhangwenjian d53c8abc6c fix🐛: generate a model and DTOs for any single-column primary key
The model and DTO templates assumed every table's key is an int column named
id. For any other key the generated backend did not compile - the model
embedded models.Model, which supplies Id, skipped the real key column, and
GetId then returned a field that did not exist ("e.Code undefined"). Past the
compile error there was more of the same assumption:

  - GetReq and UpdateReq bound the key with uri:"<json field>", while every
    generated router registers it as :id, so any other name bound nothing;
  - InsertReq tagged the key json:"-", so a key the database does not assign,
    such as a string, could never be supplied;
  - DeleteReq took Ids []int, so a string key could not be deleted.

models.Model now stands in for the key only when the key is exactly an int
named Id; anything else is declared as its own primaryKey field. The key is
bound from :id, accepted from the request body when it is not an int, and
deleted by a list of its own type. A table keyed on an int id renders
byte-for-byte as before.
2026-09-25 19:59:59 +08:00

68 lines
2.0 KiB
Plaintext

package models
import (
{{- $bb := false -}}
{{- range .Columns -}}
{{- $z := .IsQuery -}}
{{- if ($z) -}}
{{- if eq .GoType "time.Time" -}}{{- $bb = true -}}{{- end -}}
{{- end -}}
{{- end -}}
{{- 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}}
}