プロジェクト

全般

プロフィール

Vote #77637

完了

MySQL: You can't specify target table for update in FROM clause

Admin Redmine さんが3年以上前に追加. 3年以上前に更新.

ステータス:
Closed
優先度:
通常
担当者:
-
カテゴリ:
Database_21
対象バージョン:
開始日:
2022/05/09
期日:
進捗率:

0%

予定工数:
category_id:
21
version_id:
119
issue_org_id:
23347
author_id:
3866
assigned_to_id:
1
comments:
2
status_id:
5
tracker_id:
1
plus1:
0
affected_version:
closed_on:
affected_version_id:
ステータス-->[Closed]

説明

The down migration for @ClearEstimatedHoursOnParentIssues@ will fail on MySQL (5.6) with the following message:

Mysql2::Error: You can't specify target table 'issues' for update in FROM clause: UPDATE `issues` SET estimated_hours = (SELECT SUM(leaves.estimated_hours) FROM issues leaves WHERE leaves.root_id = issues.root_id AND leaves.lft > issues.lft AND leaves.rgt < issues.rgt AND leaves.rgt = leaves.lft + 1) WHERE (rgt > lft + 1)

Our quick fix was to explicitly make a temp table instead of referencing the issues table in the subquery:


diff --git a/db/migrate/20150525103953_clear_estimated_hours_on_parent_issues.rb b/db/migrate/20150525103953_clear_estimated_hours_on_parent_issues.rb
index 8eed815..c00ada0 100644
--- a/db/migrate/20150525103953_clear_estimated_hours_on_parent_issues.rb
+++ b/db/migrate/20150525103953_clear_estimated_hours_on_parent_issues.rb
@@ -6,7 +6,7 @@ class ClearEstimatedHoursOnParentIssues < ActiveRecord::Migration

   def self.down
     table_name = Issue.table_name
-    leaves_sum_select = "SELECT SUM(leaves.estimated_hours) FROM #{table_name} leaves" +
+    leaves_sum_select = "SELECT SUM(leaves.estimated_hours) FROM (SELECT * FROM #{table_name}) AS leaves" +
       " WHERE leaves.root_id = #{table_name}.root_id AND leaves.lft > #{table_name}.lft AND leaves.rgt < #{table_name}.rgt" +
       " AND leaves.rgt = leaves.lft + 1"


journals

--------------------------------------------------------------------------------
Committed, thanks.
--------------------------------------------------------------------------------

Admin Redmine さんが3年以上前に更新

  • カテゴリDatabase_21 にセット
  • 対象バージョン3.4.0_119 にセット

他の形式にエクスポート: Atom PDF

いいね!0
いいね!0