GoFrame框架使用WherePri报错原因

在使用模型的WherePri方法中,发现有时会出现SQL报错的情况,比如在做update时,有如下报错信息

there should be WHERE condition statement for UPDATE operation

表面意思是更新操作需要有where条件,说明我们通过WherePri并没有帮我们拼接以主键查询的条件。
比如我通过模型的One方法查询出一条数据,直接使用他查到的结果去做一个更新操作,就会出现这个问题,原因在于同意One方法查询到的结果是gdb.Record类型,而gdb.Record类型本质是map[string]Value的别名,gdb.Value类型是个指针,在gdb包的gdb.go文件,约666-684行有定义,所以需要将gdb.Value通过gvar包的string或者intint64等方法转化类型后,再带进WherePri方法中,才能正常拼接出where条件

type (
	// Raw is a raw sql that will not be treated as argument but as a direct sql part.
	Raw string

	// Value is the field value type.
	Value = *gvar.Var

	// Record is the row record of the table.
	Record map[string]Value

	// Result is the row record array.
	Result []Record

	// Map is alias of map[string]interface{}, which is the most common usage map type.
	Map = map[string]interface{}

	// List is type of map array.
	List = []Map
)
posted @ 2025-08-21 17:12  代码小伙  阅读(13)  评论(0)    收藏  举报