From 389a302d38235c56ff79fd9a2393f3aa56e350cf Mon Sep 17 00:00:00 2001 From: moein Date: Mon, 5 May 2025 15:00:57 +0330 Subject: [PATCH] fix: add error for multiple rows found --- errors.go | 2 ++ finisher_api.go | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/errors.go b/errors.go index 025f5d64..8140fd99 100644 --- a/errors.go +++ b/errors.go @@ -51,4 +51,6 @@ var ( ErrForeignKeyViolated = errors.New("violates foreign key constraint") // ErrCheckConstraintViolated occurs when there is a check constraint violation ErrCheckConstraintViolated = errors.New("violates check constraint") + // ErrMultipleRecordsFound occurs when more than one record found with Only + ErrMultipleRecordsFound = errors.New("multiple records found") ) diff --git a/finisher_api.go b/finisher_api.go index 5bf91af6..bb298c3d 100644 --- a/finisher_api.go +++ b/finisher_api.go @@ -181,7 +181,7 @@ func (db *DB) Only(dest interface{}, conds ...interface{}) (tx *DB) { } if count > 1 { - findTx.AddError(fmt.Errorf("expected exactly one record, but found %d", count)) + findTx.AddError(ErrMultipleRecordsFound) } return findTx