-
-
Notifications
You must be signed in to change notification settings - Fork 4k
query tables and conditions not cleared between different calls #7437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
looks like |
In GORM, if a struct already contains a non-zero primary key value, it will automatically add a Example Behavior: var user TestUser
user.ID = 1
db.First(&user) // Executes: WHERE id = 1
db.Where("id = 2").First(&user)
// Unexpected: Executes WHERE id = 2 AND id = 1
user.ID = 0
db.Where("id = 2").First(&user)
// Expected: Executes WHERE id = 2 Reason: Solutions:
|
@zwell Can you provide a link to the documentation which explains that the
Personally, if this behavior is really intentional, I would regard it as a design flaw, as it violates the time-honored Principle of Least Astonishment in a number of ways. I mean, seriously, if the program already knows the primary key for the desired row, what would be the incentive for invoking the |
API documentation I found that this section is mentioned in the documentation, though it's not very prominent. Do you have any recommendations for optimizing this design? |
That quoted passage appears in the informal general guides, not in the actual API Documentation itself published on the Go website. My recommendation would be for the method to behave exactly as the API documentation says it will. The values of the first of the rows which match the conditions passed to the |
I think the key issue is that the primary key is automatically added as a condition during queries, which can be confusing. @jinzhu Maybe it would be better to add a configuration option for this behavior, or at least make it more explicitly documented in the API. |
Your Question
I'm upgrading from gorm v1 to v2 and is totally confused about how I'm supposed to start a new query fresh. In v1, I can simply do
db.Where("id=?", id).First(&user)
, which would result in a query likeselect * from users where id = ?
no matter how many times it's called. With v2, that stopped working, seems like every time it's called, the condition is appended to the condition from last call. So if I call the query withid = 10
on the first time andid = 20
on the second time. The sql query would becomeselect * from users where id = 10 and id = 20
and it will return no rows as a result. The method chaining doc seems to have warned about this behavior. But the suggested Session(&gorm.Session{}) doesn't seem to work all the time.If I call
db.Create(user)
, thendb.Save(project)
, the second call will create sql to insert into the users table instead of projects table! That's totally unexpected . It appearsSession(&gorm.Session{NewDB:true}
seems to solve the problem. But the documentation says it will start a new db connection. So it won't work if I want multiple queries to be in one transaction?The document you expected this should be explained
Expected answer
The document needs to further explain what does
Session{}
andSession{NewDB: true}
will do. And some sample code that's more production ready (like reuse the same connection, multiple queries in the same transaction, etc.)The text was updated successfully, but these errors were encountered: