GORM Playground Link

https://github.com/go-gorm/playground/pull/1

Description

用DB.DBLink.Where(“id=? and nickname=?”, 7,'nickname23223').Find(dest)生成的sql语句出错:

SELECT * FROM players WHERE (id=(7,'nickname23223') and nickname=?) AND players.deleted_at IS NULL

Comment From: AnkoGo123

1.22.4版本

Comment From: AnkoGo123

原因找到了,因为你们没做判断,我的代码是这样写的:

//查询
func GetPlayers(dest interface{}, query interface{}, args ...interface{}) error {
    if dest == nil {
        panic("dest不可以为nil")
    }
    result := DB.DBLink.Where(query, args).Find(dest)//args后没加3个点,但是没有报错
    fmt.Println("dest:", dest)
    return result.Error
}

但是如果在args后面加上解包的3个点号是可以正常执行的,这个是项目的bug,而不是我写的代码有问题:

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: jinzhu

this is your code issues

Comment From: AnkoGo123

this is your code issues

this is your issues,not mine,You make me sick! s b pro!

Comment From: jinzhu

你这个问题就是 Go 基础不牢。。。和 GORM 没啥关系

On Sat, Feb 19, 2022 at 6:02 PM AnkoGo123 @.***> wrote:

this is your code issues

this is your issues,not mine,You make me sick!

— Reply to this email directly, view it on GitHub https://github.com/go-gorm/gorm/issues/5086#issuecomment-1045981985, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAABVOZGEDJ2CXCEGHJK7HTU35TE5ANCNFSM5OXMTXDQ . You are receiving this because you modified the open/close state.Message ID: @.***>

-- Best regards


Jinzhu github.com/jinzhu

Comment From: AnkoGo123

你看下这个东西:

//查询
func GetPlayers(dest interface{}, query interface{}, args ...interface{}) error {
    if dest == nil {
        panic("dest不可以为nil")
    }
    result := DB.DBLink.Where(query, args...).Find(dest)//星号
    fmt.Println("dest:", dest)
    return result.Error
}

上面星号处说明:如果args不加点号也是不会报错的,同时你的Where方法的args不可能是切片,你只需要判断如果是切片的类型则取出切片进行解包即可,这个是因为你的where 的参数args是...interface{}类型,而我传递进去[]interface{}并没有报错,如果是非interface{}都会给我报错并且提示我需要解包,但是这个是因为你的函数签名类型问题导致ide并没有给我提示任何报错,一门语言的类型系统就是为了防止出我这种错误 ,但是因为go的类型不完善,导致你需要进行额外的判断,这个应该是由你的框架底层来进行判断,而不是我使用出错了都无法获得ide的提示,同时你的框架也没做判断,所以这是我代码写错了,而你的类型系统导致ide没提示,同时你没做检测,所以你的框架是有问题的,这样表达我相信很容易理解吧?

Comment From: jinzhu

同时你的Where方法的args不可能是切片

where 参数类型可以是 []interface

Comment From: AnkoGo123

同时你的Where方法的args不可能是切片

where 参数类型可以是 []interface

可以给个where 参数args类型是 []interface的例子吗?非常感谢