Skip to content
New issue

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

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

Already on GitHub? # to your account

feat: 复现关联关系bug #769

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lz-freedom
Copy link

@lz-freedom lz-freedom commented Nov 7, 2024

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

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

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"` // 公司名称
}

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant