https://github.com/go-gorm/playground/pull/414

Hi, gorm cant detect proper foreign key in case of field embedding. How to fix it?

type A struct {
    ID uint
}

type B struct {
    A `gorm:"embedded"`
}

type C struct {
    ID uint
    B  B
}

func Migrate(db *gorm.DB) error {
    return db.AutoMigrate(
        B{},
        C{},
    )
}

...

invalid field found for struct models.C's field B: define a valid foreign key for relations or implement the Valuer/Scanner interface
panic: interface conversion: interface {} is models.C, not string [recovered]
    panic: interface conversion: interface {} is models.C, not string

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: jinzhu

Hi @gstarikov

In your issues, the problem is not caused by embedding, but B, C doesn't have a valid foreign key definition.

Checkout how to define a valid belongs to, has one association from https://gorm.io/docs/has_one.html