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
Closed

Can't destroy object for version 6.0.0 #206

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

Comments

@xiaohui-zhangxh
Copy link

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
Copy link
Author

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
Copy link
Collaborator

mceachen commented May 31, 2016 via email

@mceachen
Copy link
Collaborator

Added reference in the readme.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants