April 7, 2020 Gorm Sql Postgresql
When you need to filter data by a list of values (for example, IDs: 1, 2, 3), you should use the ANY operator combined with pq.Array from the PostgreSQL driver.
Here’s how to filter records by a list of IDs:
var priceIDs = []uint{1, 2, 3}
if err := db.Model(prices).Where("id = ANY(?)", pq.Array(priceIDs)).Find(&prices).Error; err != nil {
return err
}
The pq.Array() function converts a Go slice into a PostgreSQL array format that can be used with the ANY operator. This approach is efficient and works well with GORM’s query builder.
This task is not that simple, especially with GORM.
Read More → Gorm Sql Postgresql