entgo field name conflict

When using Facebook's open source orm framework ent., avoid defining the field name as Label. Otherwise, the following exceptions will be caused when the instance is migrated to the database:

ent/product/where.go:102:6: Label redeclared in this block

For example, define a product table:
{project}/ent/schema/product.go:

package schema

import (
	"github.com/facebook/ent"
	"github.com/facebook/ent/schema/field"
)

// Product holds the schema definition for the Product entity.
type Product struct {
    
    
	ent.Schema
}

// Fields of the Product.
func (Product) Fields() []ent.Field {
    
    
	return []ent.Field{
    
    
		field.String("name"),  // 产品名称
		field.String("label"),  // 产品标签
	}
}

// Edges of the Product.
func (Product) Edges() []ent.Edge {
    
    
	return nil
}

Run the go generate ./entcommand to generate the corresponding orm code, and a package named product will be generated under the ent path. The package contains the file product.go named after the entity name and the entity field filtering tool where.go
. The basic entity is defined in product.go Field:

// Code generated by entc, DO NOT EDIT.

package product

const (
	// Label holds the string label denoting the product type in the database.
	Label = "product"
	// FieldID holds the string denoting the id field in the database.
	FieldID = "id"
	// FieldName holds the string denoting the name field in the database.
	FieldName = "name"
	// FieldLabel holds the string denoting the label field in the database.
	FieldLabel = "label"

	// Table holds the table name of the product in the database.
	Table = "products"
)

// Columns holds all SQL columns for product fields.
var Columns = []string{
    
    
	FieldID,
	FieldName,
	FieldLabel,
}

// ValidColumn reports if the column name is valid (part of the table columns).
func ValidColumn(column string) bool {
    
    
	for i := range Columns {
    
    
		if column == Columns[i] {
    
    
			return true
		}
	}
	return false
}

You can see that a constant named Label is defined inside.
In where.go, related filtering methods are defined for the Label field of the entity:

// Code generated by entc, DO NOT EDIT.

package product

import (
	"goorm/ent/predicate"

	"github.com/facebook/ent/dialect/sql"
)

...

// Label applies equality check predicate on the "label" field. It's identical to LabelEQ.
func Label(v string) predicate.Product {
    
    
	return predicate.Product(func(s *sql.Selector) {
    
    
		s.Where(sql.EQ(s.C(FieldLabel), v))
	})
}

...

// LabelEQ applies the EQ predicate on the "label" field.
func LabelEQ(v string) predicate.Product {
    
    
	return predicate.Product(func(s *sql.Selector) {
    
    
		s.Where(sql.EQ(s.C(FieldLabel), v))
	})
}
...

Where.go first defines a method of the same name with the first letter capitalized for the label field Labelto compare whether the values ​​are equal.
This will result in two Labels in the product package. Therefore, performing database migration will cause the following exceptions:

ent/product/where.go:102:6: Label redeclared in this block
        previous declaration at ent/product/product.go:7:10

In this case, just replace the label field with another name, such as product_label.

Guess you like

Origin blog.csdn.net/JosephThatwho/article/details/109066460