#4175·ent

bug: cannot use id field for both primary key and foreign key

Author: afifurrohman-idCreated Aug 14, 2024Updated Feb 26, 2026
  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior

entc/gen: set "Foo" foreign-keys: field "id" was not found in Bar.Fields() for edge "foo"

Expected Behavior

Should can, i want to use id as both primary and foreign key like normal SQL this can be helpful for m2m

Steps to Reproduce

Steps:

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

// Fields of the Bar.
func (Bar) Fields() []ent.Field {
        return []ent.Field{
                field.UUID("id", uuid.UUID{}).Unique(),
                field.String("comment").NotEmpty(),
        }
}

// Edges of the Bar.
func (Bar) Edges() []ent.Edge {
        return []ent.Edge{
                edge.From("foo", Foo.Type).Ref("bar").Unique().Field("id").Required(),
        }
}


type Foo struct {
        ent.Schema
}

// Fields of the Foo.
func (Foo) Fields() []ent.Field {
        return []ent.Field{
                field.UUID("id", uuid.UUID{}).Unique(),
                field.String("comment").NotEmpty(),
        }
}

// Edges of the Foo.
func (Foo) Edges() []ent.Edge {
        return []ent.Edge{
                edge.To("bar", Bar.Type),
        }
}

Your Environment

Tech Version
Go go version go1.22.3 linux/amd64
Ent v0.14.0
Database Postgres
Driver None