diff --git a/orm/orm_queryset.go b/orm/orm_queryset.go index 802a1fe0..575f62ae 100644 --- a/orm/orm_queryset.go +++ b/orm/orm_queryset.go @@ -192,16 +192,18 @@ func (o *querySet) All(container interface{}, cols ...string) (int64, error) { // query one row data and map to containers. // cols means the columns when querying. func (o *querySet) One(container interface{}, cols ...string) error { + o.limit = 1 num, err := o.orm.alias.DbBaser.ReadBatch(o.orm.db, o, o.mi, o.cond, container, o.orm.alias.TZ, cols) if err != nil { return err } - if num > 1 { - return ErrMultiRows - } if num == 0 { return ErrNoRows } + + if num > 1 { + return ErrMultiRows + } return nil } diff --git a/orm/orm_test.go b/orm/orm_test.go index 3b854f5c..11f6bd56 100644 --- a/orm/orm_test.go +++ b/orm/orm_test.go @@ -993,12 +993,19 @@ func TestOne(t *testing.T) { var user User qs := dORM.QueryTable("user") err := qs.One(&user) - throwFail(t, AssertIs(err, ErrMultiRows)) + throwFail(t, err) user = User{} err = qs.OrderBy("Id").Limit(1).One(&user) throwFailNow(t, err) throwFail(t, AssertIs(user.UserName, "slene")) + throwFail(t, AssertNot(err, ErrMultiRows)) + + user = User{} + err = qs.OrderBy("-Id").Limit(100).One(&user) + throwFailNow(t, err) + throwFail(t, AssertIs(user.UserName, "nobody")) + throwFail(t, AssertNot(err, ErrMultiRows)) err = qs.Filter("user_name", "nothing").One(&user) throwFail(t, AssertIs(err, ErrNoRows))