zeromicro / go-zero

A cloud-native Go microservices framework with cli tool for productivity.
https://go-zero.dev
MIT License
29.44k stars 3.98k forks source link

A little problem about the function "QueryRowNoCacheCtx" and "QueryRowsNoCacheCtx" #3748

Open charfole opened 1 year ago

charfole commented 1 year ago

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: 1701181678991

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".

resOrder, err := l.svcCtx.OrderModel.FindOneByUid(l.ctx, in.Uid)
fmt.Println("Latest Order: ", resOrder)
if err != nil {
    return fmt.Errorf("Order doesn't exist.")
}

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😊🙌

github-actions[bot] commented 4 days ago

This issue is stale because it has been open for 30 days with no activity.