Skip to content

Proposal: CollectOneRow and QueryRow should return an error if multiple rows are returned #1411

Closed as not planned
@jackc

Description

@jackc

Is your feature request related to a problem? Please describe.

CollectOneRow() and QueryRow() return an error if no rows are returned by the query. However, if multiple rows are returned the first row is used and all other rows are ignored. These functions imply that the caller expects exactly one row in response. Multiple rows being returned is (almost?) always a bug.

Describe the solution you'd like

A new error (e.g. pgx.ErrTooManyRows) should be returned if more than one row is found. The first row would still be scanned or returned.

Describe alternatives you've considered

  • New functions could be introduced with this behavior. e.g. CollectExactlyOneRow() or CollectOneRowStrict(). QueryRow would be left alone in this case.
  • A strict mode flag could be added to connection config that enabled / disabled this behavior. (I think this is probably a bad idea.)

Additional context

This is a behavior change so it's arguable whether this change should be allowed in a minor version change. But I can't think of any legitimate use for the existing behavior. I think it is always a bug and any breakage would actually be revealing that bug.

Also, it's not clear to me whether, how, or if this behavior should cascade to database/sql (stdlib).

Metadata

Metadata

Assignees

No one assigned

    Labels

    RFCrequesting feedback for proposed change

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions