conflicto de nombre de campo entgo

Cuando utilice la entrada de marco de trabajo de código abierto de Facebook, evite definir el nombre del campo como Label. De lo contrario, se producirán las siguientes excepciones cuando la instancia se migre a la base de datos:

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

Por ejemplo, defina una tabla de productos:
{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
}

Ejecute el go generate ./entcomando para generar el código orm correspondiente, y se generará un paquete llamado producto bajo la ruta ent. El paquete contiene el archivo product.go con el nombre del nombre de la entidad y la herramienta de filtrado del campo de la entidad where.go
. La entidad básica es definido en 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
}

Puede ver que una constante denominada Etiqueta está definida dentro.
En where.go, los métodos de filtrado relacionados se definen para el campo Etiqueta de la entidad:

// 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 primero define un método del mismo nombre con la primera letra en mayúscula para el campo de etiqueta Labelpara comparar si los valores son iguales.
Esto dará como resultado dos etiquetas en el paquete del producto. Por lo tanto, realizar la migración de la base de datos provocará las siguientes excepciones:

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

En este caso, simplemente reemplace el campo de la etiqueta con otro nombre, como product_label.

Supongo que te gusta

Origin blog.csdn.net/JosephThatwho/article/details/109066460
Recomendado
Clasificación