简体   繁体   中英

Golang Gorm: Same query constructed differently throwing different results

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?

All the examples of using query.Where() in the documentation ( https://gorm.io/docs/query.html ) show chaining, like in your second example.

I conclude that Where() does not modify the SQL of the instance query , it returns a new query instance with the condition included.

I don't know where the deleted_at IS NULL is coming from

You may be extending your model with gorm.Model which include DeleteAt field which is cause of deleted_at IS NULL being added to the query. DeleteAt is used to check if the record is deleted then not include it in result

no mention of the LIKE clause

var artist entities.Artist

query := as.db.Model(&artist)

query.Where("name LIKE ?", "%roll%") <- will return new query
if err := query.Find(&as.Artists).Error; err != nil {
    return err
}

As Bill pointed Where returns new query with modified conditions however since you are not assigning it anything it will not be added to query

var artist entities.Artist

query := as.db.Model(&artist)

if err := query.Where("name LIKE ?", "%roll%").Find(&as.Artists).Error; err != nil {
    return err
}

Here you are using Find over the newly returned query which includes the condition hence it works as expected.

To chain multiple where condition in queries you can achieve in various ways:

1:

var artist entities.Artist

query := as.db.Model(&artist)

query = query.Where("name LIKE ?", "%roll%")

if err := query.Find(&as.Artists).Error; err != nil {
    return err
}

2:

var artist entities.Artist

query := as.db.Model(&artist)

err := query.
          Where("name LIKE ?", "%roll%").
          Where("name LIKE ?", "%m%").
          Find(&as.Artists).
          Error
if err != nil {
    return err
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM