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

是否有postload方法 #4899

Open
jianfengye opened this issue Dec 5, 2021 · 8 comments
Open

是否有postload方法 #4899

jianfengye opened this issue Dec 5, 2021 · 8 comments
Assignees
Labels
type:question general questions

Comments

@jianfengye
Copy link

jianfengye commented Dec 5, 2021

Your Question

gorm有preload的方法,但是有的时候,我已经获取到了model或者model数组,想要加载其中的某个关联字段

比如

type Answer struct {
	ID         int64          `gorm:"column:id;primaryKey"`
	QuestionID int64          `gorm:"column:question_id;index;comment:问题id;not null;default 0"`
	Context    string         `gorm:"column:context;comment:内容"`
	AuthorID   int64          `gorm:"column:author_id;comment:作者id;not null;default:0"`
	CreatedAt  time.Time      `gorm:"column:created_at;autoCreateTime;comment:创建时间"`
	UpdatedAt  time.Time      `gorm:"column:updated_at;autoUpdateTime;autoCreateTime;<-:false;comment:更新时间"`
	DeletedAt  gorm.DeletedAt `gorm:"index"`
	Author     *user.User     `gorm:"foreignKey:ID;references:author_id"`
	Question   *Question      `gorm:"foreignKey:QuestionID"`
}

我已经获取到了as := []Answers,之后我想要加载Author字段,目前我只能使用

if err := q.ormDB.WithContext(ctx).Preload("Author").Order("created_at desc").Find(answers, ids).Error; err != nil {
		return err
	}

但是这个会增加一次

{"begin":"2021-12-05T18:24:38.063506 08:00","error":null,"level":7,"msg":"orm trace sql","rows":6,"sql":"SELECT * FROM `answers` WHERE `answers`.`id` IN (25,24,23,22,21,20) AND `answers`.`deleted_at` IS NULL ORDER BY created_at desc","time":1234329,"timestamp":"2021-12-05T18:24:38 08:00"}

有没有办法有PostLoad之类的请求?还是我的使用有什么问题?

The document you expected this should be explained

Expected answer

@jianfengye jianfengye added the type:question general questions label Dec 5, 2021
@ghost
Copy link

ghost commented Dec 5, 2021

@jianfengye
Copy link
Author

这个还是preload,比如

// AnswersLoadAuthor 批量加载Author字段
func (q *QaService) AnswersLoadAuthor(ctx context.Context, answers *[]*Answer) error {
	if answers == nil {
		return nil
	}
	answerColl := collection.NewObjPointCollection(*answers)
	ids, err := answerColl.Pluck("ID").ToInt64s()
	if err != nil {
		return err
	}
	if len(ids) == 0 {
		return nil
	}
	q.logger.Debug(ctx, "ids", map[string]interface{}{
		"ids": ids,
	})

	if err := q.ormDB.WithContext(ctx).Preload("Author").Order("created_at desc").Find(answers, ids).Error; err != nil {
		return err
	}
	return nil
}

这个函数,参数已经传递了answers了,我只是要填充answers中的Author字段,但是这个sql语句会有两个语句:

select * from answers where id in (1,2,3)
select * from users where id in (11,12,13)

但是里面的第一个sql语句其实我是不想要的,所以我想,已经有了一个ansewers数组了,能不能有一个postload,能只加载Author,就是加载第二个sql语句?

@ghost
Copy link

ghost commented Dec 5, 2021

自定义预加载 的SQL 试试呢~

@jianfengye
Copy link
Author

自定义预加载 的SQL 试试呢~

能再说的详细一些么?我理解预加载的sql是做不到的

@ghost
Copy link

ghost commented Dec 5, 2021

自定义预加载 的SQL 试试呢~

能再说的详细一些么?我理解预加载的sql是做不到的

抱歉,我理解错了,这的确不行。:zipper_mouth_face:

@github-actions
Copy link

github-actions bot commented Feb 4, 2022

This issue has been automatically marked as stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 30 days

@jinzhu
Copy link
Member

jinzhu commented Feb 19, 2022

Maybe we can add a Load function to the Association Mode

@qeq66
Copy link

qeq66 commented Sep 6, 2022

@jinzhu 什么时候可以安排上,急需这个方法

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question general questions
Projects
None yet
Development

No branches or pull requests

3 participants