I want to run the following query:
SELECT *
FROM artists
WHERE name LIKE '%roll%';
the WHERE clause will be dynamic, meaning that I'll iterate through a map to build it and chain it to the main query.
Having said that, before even trying the iterate I thought about testing the method chaining (I'm new with Gorm) so I ran:
var artist entities.Artist
query := as.db.Model(&artist)
query.Where("name LIKE ?", "%roll%")
if err := query.Find(&as.Artists).Error; err != nil {
return err
}
as you can see I'm chaining different parts of the query and finalizing it with Find. This is returning all elements in the table. After printing out the executed query I'm getting:
SELECT * FROM `artists` WHERE `artists`.`deleted_at` IS NULL
no mention of the LIKE clause, furthermore, I don't know where the deleted_at IS NULL is coming from. Although at the moment it doesn't matter since ultimately I was gonna add that to the query as well.
But if I run:
var artist entities.Artist
query := as.db.Model(&artist)
if err := query.Where("name LIKE ?", "%roll%").Find(&as.Artists).Error; err != nil {
return err
}
I'm getting the results I'm expecting. The executed query is:
SELECT * FROM `artists` WHERE `artists`.`deleted_at` IS NULL AND ((name LIKE '%roll%'))
Any idea what's going on?