Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

多层嵌套关联,部分模型缺失关联关系 #1234

Open
lz-freedom opened this issue Nov 7, 2024 · 0 comments
Open

多层嵌套关联,部分模型缺失关联关系 #1234

lz-freedom opened this issue Nov 7, 2024 · 0 comments

Comments

@lz-freedom
Copy link

lz-freedom commented Nov 7, 2024

GORM Playground Link

复现代码
go-gorm/playground#769

Description

模型嵌套关联,部分模型关联关系缺失

下面代码中UserExt能通过User 关联到UserAccountRelation 下的Account 甚至在下一层的Company
但是User 却不能通过UserAccountRelation 关联到Account

qUser := query.Use(dal.DB).User
qUserExt := query.Use(dal.DB).UserExt
qUser := query.Use(dal.DB).User
  qUserExt := query.Use(dal.DB).UserExt
      qUser.WithContext(context.Background()).Preload(
      qUser.UserAccountRelationInfo,
      qUser.UserAccountRelationInfo.AccountInfo, // 此地爆红,没有AccountInfo
  ).Find()
  
  qUserExt.WithContext(context.Background()).Preload(
      qUserExt.UserInfo,
      qUserExt.UserInfo.UserAccountRelationInfo,
      qUserExt.UserInfo.UserAccountRelationInfo.AccountInfo,
      qUserExt.UserInfo.UserAccountRelationInfo.AccountInfo.CompanyInfo,
  ).Find()

生成方式

g := gen.NewGenerator(gen.Config{
    OutPath: "./dal/query",
    Mode:    gen.WithDefaultQuery, /*WithQueryInterface, WithoutContext*/
    
    WithUnitTest: true,
})
g.UseDB(dal.DB)

g.ApplyBasic(model.User{})
g.ApplyBasic(model.UserExt{})
g.ApplyBasic(model.UserAccountRelation{})
g.ApplyBasic(model.Account{})
g.ApplyBasic(model.Company{})

g.Execute()

模型关系

type User struct {
	gorm.Model
	UserMainID uint   `gorm:"column:user_main_id;not null;" json:"user_main_id"` // 用户主表ID
	Username   string `gorm:"column:username;not null;" json:"username"`         // 用户名

	UserExtInfo             *UserExt             `gorm:"foreignKey:UserID;" json:"user_ext_info"`              // 用户扩展信息,关联user_ext表
	UserAccountRelationInfo *UserAccountRelation `gorm:"foreignKey:UserID;" json:"user_account_relation_info"` // 用户账户关联信息,关联user_account_relation表
}

type UserExt struct {
	gorm.Model
	UserID uint `gorm:"column:user_id;not null;" json:"user_id"` // 用户ID

	UserInfo *User `gorm:"foreignKey:UserID;references:ID" json:"user_info"` // 用户信息,关联user表
}

type UserAccountRelation struct {
	gorm.Model

	UserID    uint `gorm:"column:user_id;not null;" json:"user_id"`       // 用户ID
	AccountID uint `gorm:"column:account_id;not null;" json:"account_id"` // 账户ID

	UserInfo    *User    `gorm:"foreignKey:UserID;references:ID" json:"user_info"`       // 用户信息,关联user表
	AccountInfo *Account `gorm:"foreignKey:AccountID;references:ID" json:"account_info"` // 账户信息,关联account表
}

type Account struct {
	gorm.Model
	CompanyID uint `gorm:"column:company_id;not null;" json:"company_id"` // 公司ID

	CompanyInfo *Company `gorm:"foreignKey:CompanyID;references:ID" json:"company_info"` // 公司信息,关联company表
}

type Company struct {
	gorm.Model
	Name string `gorm:"column:name;not null;" json:"name"` // 公司名称
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant