Lately I've been learning the gozero official recommended project "zeromall". When I'm doing some queries on the following order table, some unexpected errors happened.
Order table structure:
I tried to write a function in ordermodel.go (generated by goctl automatically) to find the latest record of a specified uid (i.e., user id) by invoking the "QueryRowsNoCacheCtx" function, an error happened and no record returned (even the record did exist in my table).
func (m *defaultOrderModel) FindOneByUid(ctx context.Context, uid int64) (*Order, error) {
var resp Order
query := fmt.Sprintf("select %s from %s where `uid` = ? order by create_time desc limit 1", orderRows, m.table)
// return an error because of no records are found
err := m.QueryRowsNoCacheCtx(ctx, &resp, query, uid)
switch err {
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}
But when I changed the function to "QueryRowNoCacheCtx" (Row without s), the error disappeared and the code worked as expected.
func (m *defaultOrderModel) FindOneByUid(ctx context.Context, uid int64) (*Order, error) {
var resp Order
query := fmt.Sprintf("select %s from %s where `uid` = ? order by create_time desc limit 1", orderRows, m.table)
// It works
err := m.QueryRowNoCacheCtx(ctx, &resp, query, uid)
switch err {
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}
By the way, the following code shows how I invoke the "FindOneByUid" function, it returns an error when invoking "QueryRowsNoCacheCtx" but is normal when invoking "QueryRowNoCacheCtx".
It really confused me because i think the "QueryRowsNoCacheCtx" should find the record and shouldn't return an error even though it is designed to do some batch queries. So my question is, is that a difference caused by some bugs or it's just the normal difference between these two functions which caused this problem?
Lately I've been learning the gozero official recommended project "zeromall". When I'm doing some queries on the following order table, some unexpected errors happened.
Order table structure:
I tried to write a function in ordermodel.go (generated by goctl automatically) to find the latest record of a specified uid (i.e., user id) by invoking the "QueryRowsNoCacheCtx" function, an error happened and no record returned (even the record did exist in my table).
But when I changed the function to "QueryRowNoCacheCtx" (Row without s), the error disappeared and the code worked as expected.
By the way, the following code shows how I invoke the "FindOneByUid" function, it returns an error when invoking "QueryRowsNoCacheCtx" but is normal when invoking "QueryRowNoCacheCtx".
It really confused me because i think the "QueryRowsNoCacheCtx" should find the record and shouldn't return an error even though it is designed to do some batch queries. So my question is, is that a difference caused by some bugs or it's just the normal difference between these two functions which caused this problem?
Thank you😊🙌