gf icon indicating copy to clipboard operation
gf copied to clipboard

orm: Model 的 Raw() 的 Count 忽略了Where 条件

Open FlashIvano opened this issue 1 month ago • 1 comments

Go version

go version go1.25.0 linux/amd64

GoFrame version

2.9.4

Can this bug be reproduced with the latest release?

Option Yes

What did you do?

	getBasicUserSql := `
	SELECT
		wf.user_id,
                ....
        FROM ....
        `
	items := []*PersonBasicInfoListItem{}

	err = platDb.Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {


		m := tx.Raw(getBasicUserSql)

m.Where("wf.user_id = ?", in.UserId)

		err = m.ScanAndCount(&items, &total, true)
		if err != nil {
			return
		}

             return
       }

What did you see happen?

用 Raw 方法时

What did you expect to see?

对于Raw方法,正确在Count时添加Where条件

FlashIvano avatar Nov 02 '25 09:11 FlashIvano

用Raw方法创建model如果传入的sql是个带where+order by+group by的完整sql,然后又调用了where,gdb没法往里面加你where进去的条件,你还是直接这么写吧

	getBasicUserSql := `
	SELECT
		wf.user_id,
                ....
        FROM ....
        WHERE wf.user_id  = ?
        `
	items := []*PersonBasicInfoListItem{}

	err = platDb.Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {

		m := tx.Raw(getBasicUserSql, in.UserId)
		err = m.ScanAndCount(&items, &total, true)
		if err != nil {
			return
		}

             return
       }

LanceAdd avatar Nov 28 '25 05:11 LanceAdd