英文:
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,
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 Token
s 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(),
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论