goent

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


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.


Solution

  • Not supported right now at 2023/1/4

    https://github.com/ent/ent/issues/2549#issuecomment-1129666251