Your Question

Example

r := roomModel.Room{
    Id: 9999999999,
}
var count int64
e := r.DB().Model(r).Where("1=1").Count(&count).Error
//SELECT count(*) FROM `room` WHERE 1=1

The document you expected this should be explained

Expected answer

SELECT count(*) FROM `room` WHERE id=9999999999 and 1=1

Comment From: black-06

Without Where("1=1"), it would be:

type Person struct {
    ID   int
    Name string
}

var c int64
db.Model(&Person{ID: 1}).Count(&c)
// SELECT count(*) FROM `people`

So you mean GORM should take pk in Model when querying?

Comment From: wlbwlbwlb

Without Where("1=1"), it would be:

``` type Person struct { ID int Name string }

var c int64 db.Model(&Person{ID: 1}).Count(&c) // SELECT count(*) FROM people ```

So you mean GORM should take pk in Model when querying?

Yes, it is

Comment From: black-06

Maybe you are right, but gorm has not done so. Is this a bug? @a631807682 . If so, I would like to fix it

Comment From: a631807682

r := roomModel.Room{ Id: 9999999999, } var count int64 e := r.DB().Model(r).Where("1=1").Count(&count).Error

It seems that the primary key will be considered as a query condition only when Update? This behavior inconsistency may cause misunderstandings, but it is difficult for us to maintain such features in insert-related APIs, and currently query-related APIs do not support it (we seem to have a reason to support it, but this may be a relatively large breaking change), can we explain it in the docs (it's just a feature of update)?

Comment From: black-06

r := roomModel.Room{ Id: 9999999999, } var count int64 e := r.DB().Model(r).Where("1=1").Count(&count).Error

It seems that the primary key will be considered as a query condition only when Update? This behavior inconsistency may cause misunderstandings, but it is difficult for us to maintain such features in insert-related APIs, and currently query-related APIs do not support it (we seem to have a reason to support it, but this may be a relatively large breaking change), can we explain it in the docs (it's just a feature of update)?

lgtm