Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
GitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
database/sql: QueryRow should verify that we have only one row #20163
// QueryRow executes a query that is expected to return at most one row. // QueryRow always returns a non-nil value. Errors are deferred until // Row's Scan method is called.
This suggests that we should get an error if the query returns more than one row. However
Conversely "at most one row" suggests that getting no rows is somehow acceptable, but an error is returned in that case. I think it should say "exactly one row".
No, it should not. If the query returns no rows, you get a ErrNoRows on Scan. That's useful and constitutes valid usage of QueryRow,
Also, I think you're reading this the wrong way: the "expected to return at most one row" part places an obligation on the user of QueryRow and not on its implementation. You're not supposed to use it in the first place if your query can return more than one row.
As the existing implementation allowed it, changing it to return an error now has the potential to break code that relied on it returning the first result (whatever that is).