How to customize the columns corresponding to the foreign keys when they are created in entgo?

913 views Asked by At

Assume the following schema

type Pet struct {
    ent.Schema
}

// Fields of the Pet.
func (Pet) Fields() []ent.Field {
    return []ent.Field{
        field.String("name"),
    }
}

// Edges of the Pet.
func (Pet) Edges() []ent.Edge {
    return []ent.Edge{
        edge.From("owner", User.Type).
            Ref("pets").
            Unique(),
    }
}
type User struct {
    ent.Schema
}

// Fields of the User.
func (User) Fields() []ent.Field {
    return []ent.Field{
        field.Int("age"),
        field.String("name").Unique(),
    }
}

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

ent will generate a foreign key such like pets_users_pets (user_pets) -> users(id). And id is the default primary key column in entgo.

But what should I do to create a (user_pets) -> users(name)? name is also a unique column.

1

There are 1 answers

0
Zxilly On BEST ANSWER