我不知道为什么在entgo中无法使用edge generate。

huangapple go评论106阅读模式
英文:

I don't know why edge generate doesn't work in entgo

问题

我正在尝试使用Golang和Entgo构建应用程序,但在Entgo生成方面遇到了问题。

下面是用户表的代码:

type User struct {
	ent.Schema
}

func (User) Fields() []ent.Field {
	return []ent.Field{
		field.String("nickname").NotEmpty().Unique(),
		field.String("user_type").Default("guest"),
		field.Time("created_at").Default(time.Now()),
		field.Time("updated_at").Default(time.Now()).UpdateDefault(time.Now()),
	}
}

func (User) Edges() []ent.Edge {
	return []ent.Edge{
		edge.To("token", Token.Type),
	}
}

下面是关于令牌的代码:

type Token struct {
	ent.Schema
}

func (Token) Fields() []ent.Field {
	return []ent.Field{
		field.String("token"),
		field.Int("user_id"),
		field.Time("created_at").Default(time.Now()),
		field.Time("updated_at").Default(time.Now()).UpdateDefault(time.Now()),
	}
}

func (Token) Edges() []ent.Edge {
	return []ent.Edge{
		edge.From("user", User.Type).Ref("users").Field("user_id"),
	}
}

我尝试像这样生成Entgo:

go generate ./ent

但是它没有起作用,并且给出了以下错误:

entc/gen: resolve "Token" relations: edge "users" is missing for inverse edge: Token.user(User)
exit status 1

我不知道出了什么问题,请帮忙解决。

英文:

i'm trying build app by golang and entgo and falling problem about entgo generate,

https://entgo.io/

below is user table code

type user table

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

// Fields of the User.
func (User) Fields() []ent.Field {
	return []ent.Field{
		field.String("nickname").NotEmpty().Unique(),
		field.String("user_type").Default("guest"),
		field.Time("created_at").Default(time.Now()),
		field.Time("updated_at").Default(time.Now()).UpdateDefault(time.Now()),
	}

}

// Edges of the User.
func (User) Edges() []ent.Edge {
	return []ent.Edge{
		edge.To("token", Token.Type),
	}	
}

and below is about token

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

// Fields of the Token.
func (Token) Fields() []ent.Field {
	return []ent.Field{
		field.String("token"),
		field.Int("user_id"),
		field.Time("created_at").Default(time.Now()),
		field.Time("updated_at").Default(time.Now()).UpdateDefault(time.Now()),
	}
}

// Edges of the Token.
func (Token) Edges() []ent.Edge {
	return []ent.Edge{
		edge.From("user", User.Type).Ref("users").Field("user_id"),
	}
}

and im try generate entgo like this

go generate ./ent

but it is not working and i give error like below,

entc/gen: resolve "Token" relations: edge "users" is missing for inverse edge: Token.user(User)
exit status 1

i dont know what

I don't know what's wrong, please help

答案1

得分: 0

你已经颠倒了edge.To和edge.From的顺序。

请参考文档中的说明:

使用edge.To构建器定义边的模式拥有该关系,而使用edge.From构建器仅为关系提供一个反向引用(具有不同的名称)。

从你的示例中看,你似乎想要实现一个一对多的关系,其中一个User可以关联多个Token

请将两个结构体上的Edges方法更改为:

// User的边。
func (User) Edges() []ent.Edge {
	return []ent.Edge{
		edge.From("token", Token.Type).
			Ref("users"),
	}
}

// Token的边。
func (Token) Edges() []ent.Edge {
	return []ent.Edge{
		edge.To("users", User.Type).
			Unique().
			Field("user_id").
			Required(),
	}
}
英文:

You've reversed the order of edge.To / edge.From.

See the ent docs:
> A schema that defines an edge using the edge.To builder owns the relation, unlike using the edge.From builder that gives only a back-reference for the relation (with a different name).

From your example it looks like you're trying to achieve a O2M relation where a User can have many Tokens associated.

Change the Edges methods on both structs to be:

// Edges of the User.
func (User) Edges() []ent.Edge {
	return []ent.Edge{
		edge.From("token", Token.Type).
			Ref("users"),
	}
}

and

// Edges of the Token.
func (Token) Edges() []ent.Edge {
	return []ent.Edge{
		edge.To("users", User.Type).
			Unique().
			Field("user_id").
			Required(),
	}
}

huangapple
  • 本文由 发表于 2022年8月3日 17:38:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/73219476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定