Copy TableExpr when clone statement

This commit is contained in:
Jinzhu 2020-08-13 16:28:21 +08:00
parent ecc946be6e
commit dea93edb6a
2 changed files with 11 additions and 0 deletions

View File

@ -392,6 +392,7 @@ func (stmt *Statement) Parse(value interface{}) (err error) {
func (stmt *Statement) clone() *Statement { func (stmt *Statement) clone() *Statement {
newStmt := &Statement{ newStmt := &Statement{
TableExpr: stmt.TableExpr,
Table: stmt.Table, Table: stmt.Table,
Model: stmt.Model, Model: stmt.Model,
Dest: stmt.Dest, Dest: stmt.Dest,

View File

@ -562,4 +562,14 @@ func TestUpdateFromSubQuery(t *testing.T) {
if result.Name != user.Company.Name { if result.Name != user.Company.Name {
t.Errorf("name should be %v, but got %v", user.Company.Name, result.Name) t.Errorf("name should be %v, but got %v", user.Company.Name, result.Name)
} }
DB.Model(&user.Company).Update("Name", "new company name")
if err := DB.Table("users").Where("1 = 1").Update("name", DB.Table("companies").Select("name").Where("companies.id = users.company_id")).Error; err != nil {
t.Errorf("failed to update with sub query, got error %v", err)
}
DB.First(&result, user.ID)
if result.Name != "new company name" {
t.Errorf("name should be %v, but got %v", user.Company.Name, result.Name)
}
} }