From dea93edb6acdccdb398a5f9d89412f9bd0be5b39 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Thu, 13 Aug 2020 16:28:21 +0800 Subject: [PATCH] Copy TableExpr when clone statement --- statement.go | 1 + tests/update_test.go | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/statement.go b/statement.go index e9d826c4..b5b5db5a 100644 --- a/statement.go +++ b/statement.go @@ -392,6 +392,7 @@ func (stmt *Statement) Parse(value interface{}) (err error) { func (stmt *Statement) clone() *Statement { newStmt := &Statement{ + TableExpr: stmt.TableExpr, Table: stmt.Table, Model: stmt.Model, Dest: stmt.Dest, diff --git a/tests/update_test.go b/tests/update_test.go index 83a7b9a2..a59a8856 100644 --- a/tests/update_test.go +++ b/tests/update_test.go @@ -562,4 +562,14 @@ func TestUpdateFromSubQuery(t *testing.T) { if result.Name != user.Company.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) + } }