简体   繁体   English

Golang Gorm:构造不同的相同查询会抛出不同的结果

[英]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. WHERE子句将是动态的,这意味着我将遍历 map 来构建它并将其链接到主查询。

Having said that, before even trying the iterate I thought about testing the method chaining (I'm new with Gorm) so I ran:话虽如此,在尝试迭代之前,我考虑过测试方法链(我是 Gorm 的新手)所以我运行了:

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 .如您所见,我链接了查询的不同部分并使用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.没有提到LIKE子句,此外,我不知道deleted_at IS NULL来自哪里。 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.文档( https://gorm.io/docs/query.html )中使用query.Where()的所有示例都显示了链接,就像您的第二个示例一样。

I conclude that Where() does not modify the SQL of the instance query , it returns a new query instance with the condition included.我得出结论, Where()不会修改实例query的 SQL,它会返回一个包含条件的新查询实例。

I don't know where the deleted_at IS NULL is coming from我不知道deleted_at IS NULL来自哪里

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.您可能正在使用gorm.Model扩展您的 model,其中包括DeleteAt字段,这是将deleted_at IS NULL添加到查询中的原因。 DeleteAt is used to check if the record is deleted then not include it in result DeleteAt用于检查记录是否被删除,然后不将其包含在结果中

no mention of the LIKE clause没有提到 LIKE 子句

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正如 Bill 指出的那样, Where会返回具有修改条件的新查询,但是由于您没有为其分配任何内容,因此不会将其添加到查询中

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.在这里,您对新返回的查询使用Find ,其中包含条件,因此它按预期工作。

To chain multiple where condition in queries you can achieve in various ways:要在查询中链接多个 where 条件,您可以通过多种方式实现:

1: 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: 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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