From 218650ab7ccfc766dff703a01c44fef994e94e58 Mon Sep 17 00:00:00 2001 From: Shaya Potter Date: Thu, 15 May 2025 17:19:14 +0300 Subject: [PATCH] Add the ability to enforce adding table columns in user defined order --- dialect/entsql/annotation.go | 8 ++++++++ entc/gen/graph.go | 3 ++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/dialect/entsql/annotation.go b/dialect/entsql/annotation.go index aae8a420ee..09dd6cddae 100644 --- a/dialect/entsql/annotation.go +++ b/dialect/entsql/annotation.go @@ -193,6 +193,10 @@ type Annotation struct { // } ViewFor map[string]string `json:"view_for,omitempty"` + // TableFieldOrder tells ent to use the user provided Field order for columns, + // and to not push all Edges till the end of the column list + TableFieldOrder bool `json:"table_field_order,omitempty"` + // error occurs during annotation build. This field is not // serialized to JSON and used only by the codegen loader. err error @@ -299,6 +303,10 @@ func ViewFor(dialect string, as func(*sql.Selector)) *Annotation { } } +func TableFieldOrder() *Annotation { + return &Annotation{TableFieldOrder: true} +} + // Default specifies a literal default value of a column. Note that using // this option overrides the default behavior of the code-generation. // diff --git a/entc/gen/graph.go b/entc/gen/graph.go index 4d6ca9d8dc..d1d606a83f 100644 --- a/entc/gen/graph.go +++ b/entc/gen/graph.go @@ -649,11 +649,12 @@ func (g *Graph) Tables() (all []*schema.Table, err error) { default: table.SetAnnotation(ant).SetSchema(ant.Schema) } + nAnt := n.EntSQL() for _, f := range n.Fields { if a := f.EntSQL(); a != nil && a.Skip { continue } - if !f.IsEdgeField() { + if (nAnt != nil && nAnt.TableFieldOrder) || !f.IsEdgeField() { table.AddColumn(f.Column()) } }