Writing a webserver in Golang with gorm & postgres, I got stuck misunderstanding what's exactly going on at the second loop iteration in the following code:
...
for _, t := range tasks {
newDbConn := db.SchoolServerDB.Debug().New()
err = newDbConn.Where("id = ?", t.DayID).First(&day).Error
if err != nil {
return errors.Wrapf(err, "Error query day with id='%v'", t.DayID)
}
...
}
...
First iteration debug:
SELECT * FROM "days" WHERE "days"."deleted_at" IS NULL AND ((id = '8')) ORDER BY "days"."id" ASC LIMIT 1
Second iteration debug:
SELECT * FROM "days" WHERE "days"."deleted_at" IS NULL AND "days"."id" = '8' AND ((id = '38')) ORDER BY "days"."id" ASC LIMIT 1
The key question is: why search conditions are accumulated despite there's a new connection being created each iteration? According to docs search conditions must be cleared every time. I'd like to get the second result like this:
SELECT * FROM "days" WHERE "days"."deleted_at" IS NULL AND ((id = '38')) ORDER BY "days"."id" ASC LIMIT 1
Any help appreciated!
UPD:
db.SchoolServerDb is just *gorm.DB and Debug() is its method
Table 'days' is made of struct Day:
type Day struct {
gorm.Model
StudentID uint // parent id
Date string `sql:"size:255"`
Tasks []Task // has-many relation
Lessons []Lesson // has-many relation
}