Your Question

In other words, in same one connection, i can use query different tables using multiple sql

db.Connection(func(tx *gorm.DB) error {
        videoModels := []biz.VideoModel{}
        tx.Scopes(Paginate(&biz.PageParam{
            PageNum:  pageIndex,
            PageSize: pageSize,
        }), WhereIfModelIds(modelIds), WhereIfModelName(name)).
            Model(biz.VideoModel{}).
            Find(&videoModels)

        tx.Scopes(WhereIfModelIds(modelIds), WhereIfModelName(name)).
            Model(biz.VideoModel{}).
            Count(&totalCount)
        // eg tx.Find other table
        return nil
    })

but, because tx is the same reference, the .Where/Scope condition still exists,making subsequent queries unavailable

The document you expected this should be explained

I want to this feature, becuase

  • one connection multiple query is common scenarios
  • performance

Expected answer

db.Connection(func(tx *gorm.DB) error {
        videoModels := []biz.VideoModel{}
        tx.Scopes(Paginate(&biz.PageParam{
            PageNum:  pageIndex,
            PageSize: pageSize,
        }), WhereIfModelIds(modelIds), WhereIfModelName(name)).
            Model(biz.VideoModel{}).
            Find(&videoModels)

        tx.ClearWhere()

        tx.Scopes(WhereIfModelIds(modelIds), WhereIfModelName(name)).
            Model(biz.VideoModel{}).
            Count(&totalCount)
        // eg tx.Find other table
        return nil
    })

i thinks create new db client, but is not raii, awalys manually release this new client。

can you tell me how to achieve it.

Comment From: MarsonShine

relation issue - https://github.com/go-gorm/gorm/issues/4513 - https://github.com/go-gorm/gorm/issues/5182

Comment From: MarsonShine

Sorry, I misread the source code, closed connect instead of gorm.db close