Hello,

we are seeing something very concerning with this code:

u := User{ID: "EXISTING_STRING_ID_IN_THE_DB"}
err = t.rdsMainDB.First(&u, "id = ?", "EXISTING_STRING_ID_IN_THE_DB").Error
log.Println(r)

returns: ErrRecordNotFound even though that is a legit id in the Users table

If the u above is initialized to an empty User object, it will work ok.

it's either a very weird bug / issue or we just can't explain why would it do something like this (could not find anything in the docs or SO)

User:

type UserID string

type User struct {
    ID        UserID         `json:"id" gorm:"primaryKey"`
    CreatedAt time.Time      `json:"created_at"`
    UpdatedAt time.Time      `json:"updated_at"`
    DeletedAt gorm.DeletedAt `json:"deleted_at"`

    Email       string `json:"email" gorm:"uniqueIndex,size:356"`
    Name        string `json:"name" gorm:"size:128"`
    Description string `json:"description" gorm:"size:512"`

        ...
}

Version:

gorm.io/driver/postgres v1.3.4
gorm.io/gorm v1.23.4

Comment From: korovkintaptap

type UserID string

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: korovkin

@jinzhu please share your thoughts

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: jinzhu

Hi @korovkin @korovkintaptap

If User is initialized with non-blank primary key, the primary key will be used to build the query condition, for example:

u := User{ID: "id1"}
err = t.rdsMainDB.First(&u, "id = ?", "id2").Error

this is generate SQL

select * from users where id = id1 and id = id2

This is the default behavior for Query, Delete, Update.

Comment From: korovkintaptap

Thank you for responding and explaining This is defiantly unexpected

What’s the rational for this behavior ? Are you sure we want to keep it?

I expected the only condition at play is the “Id = ?” that I specifically write …

On Apr 23, 2022, at 18:45, Jinzhu @.***> wrote:

 Hi @korovkin @korovkintaptap

If User is initialized with non-blank primary key, the primary key will be used to build the query condition, for example:

u := User{ID: "id1"} err = t.rdsMainDB.First(&u, "id = ?", "id2").Error this is generate SQL

select * from users where id = id1 and id = id2 This is the default behavior for Query, Delete, Update.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.

Comment From: jinzhu

Hi @korovkintaptap

Can you let me know the reason to use an initialized object?

Comment From: korovkin

the object is coming in from an endpoint ... thus sometimes can be initialized and sometimes not (whatever the JSON payload that was sent with the request)

thus the big surprise, i would have never guessed that the destination where the object will be read into from the DB matters.

On Sat, Apr 23, 2022 at 8:03 PM Jinzhu @.***> wrote:

Hi @korovkintaptap https://github.com/korovkintaptap

Can you let me know the reason to use an initialized object?

— Reply to this email directly, view it on GitHub https://github.com/go-gorm/gorm/issues/5285#issuecomment-1107694000, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABAIKGPBFHYK2GJYX73PB3LVGS2XHANCNFSM5T75CFPA . You are receiving this because you were mentioned.Message ID: @.***>

-- Best Regards, H