Can't destroy object for version 6.0.0
xiaohui-zhangxh opened this issue · 3 comments
xiaohui-zhangxh commented
Please see below output, am I missing something to configure model?
module Catalog
class Menu < ActiveRecord::Base
acts_as_tree dependent: :destroy
end
end
2.2.1 :002 > Catalog::Menu.find(4).destroy
Catalog::Menu Load (0.4ms) SELECT `catalog_menus`.* FROM `catalog_menus` WHERE `catalog_menus`.`id` = 4 LIMIT 1
(0.2ms) BEGIN
(0.3ms) SELECT GET_LOCK('ClosureTree::Catalog::Menu', 0) AS t457e5acb1b089fcf2c797b4a5f36d844
(12.3ms) DELETE FROM `catalog_menu_hierarchies`
WHERE descendant_id IN (
SELECT DISTINCT descendant_id
FROM (SELECT descendant_id
FROM `catalog_menu_hierarchies`
WHERE ancestor_id = 4
OR descendant_id = 4
) AS x )
(0.3ms) SELECT RELEASE_LOCK('ClosureTree::Catalog::Menu') AS t5d47f4adccd01cbd21808d1b3cb19864
(5.3ms) ROLLBACK
ActiveRecord::StatementInvalid: Mysql2::Error: You can't specify target table 'catalog_menu_hierarchies' for update in FROM clause: DELETE FROM `catalog_menu_hierarchies`
WHERE descendant_id IN (
SELECT DISTINCT descendant_id
FROM (SELECT descendant_id
FROM `catalog_menu_hierarchies`
WHERE ancestor_id = 4
OR descendant_id = 4
) AS x )
xiaohui-zhangxh commented
Ok, I found the solution by my self. This error happens if MySQL is 5.7+, SET optimizer_switch = 'derived_merge=off'
can get rid of error. or set my.cnf
optimizer_switch=derived_merge=off
under [mysqld]
section.
This is where I get the solution: http://stackoverflow.com/questions/32528773/mysql-5-5-to-5-7-you-cant-specify-target-table-for-update-in-from-clause-sa
mceachen commented
Thanks for sharing the solution!
mceachen commented
Added reference in the readme.