mbleigh / acts-as-taggable-on

A tagging plugin for Rails applications that allows for custom tagging along dynamic contexts.
http://mbleigh.lighthouseapp.com/projects/10116-acts-as-taggable-on
MIT License
4.97k stars 1.2k forks source link

Errors when trying to execute .count in the result of .tagged_with #1109

Open fguillen opened 1 year ago

fguillen commented 1 year ago

Rails: 7.0.2.2 Ruby: 3.1.1p18 Mysql: 8.0.33 acts-as-taggable-on: 9.0.1

irb(main):014:0> Article.tagged_with("tag_1").count
  Article Count (1.0ms)  SELECT COUNT(*) FROM `articles` INNER JOIN `taggings` `article_taggings_f30f826` ON `article_taggings_f30f826`.`taggable_id` = `articles`.`uuid` AND `article_taggings_f30f826`.`taggable_type` = 'Article' AND `article_taggings_f30f826`.`tag_id` IN (SELECT `tags`.`id` FROM `tags` WHERE LOWER(`tags`.`name`) LIKE 'tag!_1' ESCAPE '!')
=> 1

Ok!

irb(main):015:0> Article.tagged_with("tag_1", any: true).count
/Users/me/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/mysql2-0.5.3/lib/mysql2/client.rb:131: warning: rb_tainted_str_new_cstr is deprecated and will be removed in Ruby 3.2
  Article Count (0.6ms)  SELECT COUNT(`articles`.*) FROM `articles` WHERE EXISTS (SELECT * FROM `taggings` WHERE `taggings`.`taggable_id` = `articles`.`uuid` AND `taggings`.`taggable_type` = 'Article' AND `taggings`.`tag_id` IN (SELECT `tags`.`id` FROM `tags` WHERE (LOWER(`tags`.`name`) LIKE 'tag!_1' ESCAPE '!')))
/Users/me/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/mysql2-0.5.3/lib/mysql2/client.rb:131:in `_query': Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*) FROM `articles` WHERE EXISTS (SELECT * FROM `taggings` WHERE `taggings`.`tagg' at line 1 (ActiveRecord::StatementInvalid)
/Users/me/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/mysql2-0.5.3/lib/mysql2/client.rb:131:in `_query': You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*) FROM `articles` WHERE EXISTS (SELECT * FROM `taggings` WHERE `taggings`.`tagg' at line 1 (Mysql2::Error)

Error!

irb(main):016:0> Article.tagged_with("tag_1", match_all: true).count
  Article Load (0.6ms)  SELECT COUNT(*) FROM `tags` WHERE (LOWER(`tags`.`name`) LIKE 'tag!_1' ESCAPE '!')
  Article Count (0.8ms)  SELECT COUNT(*) AS `count_all`, `articles`.`uuid` AS `articles_uuid` FROM `articles` INNER JOIN `taggings` `article_taggings_f30f826` ON `article_taggings_f30f826`.`taggable_id` = `articles`.`uuid` AND `article_taggings_f30f826`.`taggable_type` = 'Article' AND `article_taggings_f30f826`.`tag_id` IN (SELECT `tags`.`id` FROM `tags` WHERE LOWER(`tags`.`name`) LIKE 'tag!_1' ESCAPE '!') LEFT OUTER JOIN `taggings` ON `taggings`.`taggable_id` = `articles`.`uuid` AND `taggings`.`taggable_type` = 'Article' GROUP BY `articles`.`uuid` HAVING COUNT(`taggings`.`taggable_id`) = (SELECT COUNT(*) FROM `tags` WHERE (LOWER(`tags`.`name`) LIKE 'tag!_1' ESCAPE '!'))
=> {"UUID_1"=>1}

Weird. I was expecting 1 not a group by uuid

irb(main):017:0> Article.tagged_with("tag_1", exclude: true).count
/Users/me/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/mysql2-0.5.3/lib/mysql2/client.rb:131: warning: rb_tainted_str_new_cstr is deprecated and will be removed in Ruby 3.2
  Article Count (0.7ms)  SELECT COUNT(*) FROM `articles` WHERE `articles`.`id` NOT IN (SELECT `taggings`.`taggable_id` FROM `taggings` INNER JOIN `tags` ON `taggings`.`tag_id` = `tags`.`id` AND `taggings`.`taggable_type` = 'Article' AND (LOWER(`tags`.`name`) LIKE 'tag!_1' ESCAPE '!'))
/Users/me/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/mysql2-0.5.3/lib/mysql2/client.rb:131:in `_query': Mysql2::Error: Unknown column 'articles.id' in 'IN/ALL/ANY subquery' (ActiveRecord::StatementInvalid)
/Users/me/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/mysql2-0.5.3/lib/mysql2/client.rb:131:in `_query': Unknown column 'articles.id' in 'IN/ALL/ANY subquery' (Mysql2::Error)

Error!

This is special because my model has not field id but uuid:

self.primary_key = :uuid