Your Question

I create BelongsTo relationship:

type A struct {
        ID            uint
        MyFavoriteBID uint
        MyFavoriteB   B
}

type B struct {
        ID uint
}

It creates correct foreign keys: Gorm How to combine HasMany and BelongsTo to the same table?

Now I add HasMany relationship:

type A struct {
        ID            uint
        MyFavoriteBID uint
        MyFavoriteB   B
        MyBs          []B
}

type B struct {
        ID  uint
        AID uint
}

Now the BelongsTo foreign key is incorrect:

** Gorm How to combine HasMany and BelongsTo to the same table? ** It doesn't references MyFavoriteBID anymore. How is it possible to combine?

The document you expected this should be explained

https://gorm.io/docs/belongs_to.html https://gorm.io/docs/has_many.html

Expected answer

Example of how to use BelongsTo and HasMany to the same table