Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't destroy object for version 6.0.0 #206

Closed
xiaohui-zhangxh opened this issue Mar 27, 2016 · 3 comments

Comments

@xiaohui-zhangxh
Copy link

commented Mar 27, 2016

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

This comment has been minimized.

Copy link
Author

commented May 31, 2016

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

This comment has been minimized.

Copy link
Collaborator

commented May 31, 2016

@mceachen

This comment has been minimized.

Copy link
Collaborator

commented Jul 21, 2016

Added reference in the readme.

@mceachen mceachen closed this Jul 21, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.