Menu

GORM counted 11 rows. PostgreSQL found 7 distinct pairs.
📰

GORM counted 11 rows. PostgreSQL found 7 distinct pairs.

DEV Community: go·Blucca·14 days ago
#BR0qtnP0
#devtogo#devto#dev#count#gorm
Reading 0:00
15s threshold

A count can execute successfully and still answer the wrong question.

In a native Go + PostgreSQL reproduction, this GORM query returned 11 :

var count int64 err := db . Table ( "fixture_pairs" ) . Distinct ( "a" , "b" ) . Count ( & count ) . Error

Enter fullscreen mode

Exit fullscreen mode

The database contained 7 distinct (a, b) pairs . err was nil. The emitted SQL was:

SELECT count ( * ) FROM "fixture_pairs" ;

Enter fullscreen mode

Exit fullscreen mode

This reproduces the public GORM issue #6798 . The measured version is commit ead4e04ba7c6a49654649131e587133b1d9f3eca , with gorm.io/driver/[email protected] and PostgreSQL 16.15.

Name the thing you are counting

Physical rows and distinct projected tuples are two different counting units. A useful regression check states the unit before invoking the ORM.

The fixture has eleven fictional records.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.