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

Invalid query when running some class methods? #32

Open
antonysastre opened this issue Jan 28, 2020 · 9 comments
Open

Invalid query when running some class methods? #32

antonysastre opened this issue Jan 28, 2020 · 9 comments

Comments

@antonysastre
Copy link

antonysastre commented Jan 28, 2020

Running: PostgreSQL 11

Gemfile.lock
    acts-as-taggable-array-on (0.6.0)
      activerecord (>= 4)
      activesupport (>= 4)
class Photo < ApplicationRecord
  taggable_array :tags
end

> Photo.last.tags => ["coffee", "sun"]
> Photo.with_any_tags("coffee") => [#<Photo: …>, #<Photo: …>]
> Photo.tags_cloud => ActiveRecord::StatementInvalid: PG::UndefinedTable

Running Photo.tags_cloud or seems to produce invalid query:

DEPRECATION WARNING: Dangerous query method (method whose arguments are used as raw SQL) called with non-attribute argument(s): "tag, count(*) as count". Non-attribute arguments will be disallowed in Rails 6.0. This method should not be called with user-provided values, such as request parameters or model attributes. Known-safe values can be passed by wrapping them in Arel.sql(). (called from __pry__ at (pry):4)

   (1.4ms)  SELECT tag, count(*) as count FROM (SELECT unnest(photos.tags) as tag FROM "photos") subquery GROUP BY tag ORDER BY "photos"."created_at" DESC, tag
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "photos" 

LINE 1: ...tag FROM "photos") subquery GROUP BY tag ORDER BY "photos"."...
                                                             ^
: SELECT tag, count(*) as count FROM (SELECT unnest(photos.tags) as tag FROM "photos") subquery GROUP BY tag ORDER BY "photos"."created_at" DESC, tag
from /Users/antonysastre/.rvm/gems/ruby-2.5.0/gems/activerecord
-5.2.0.rc2/lib/active_record/connection_adapters/postgresql_adapter.rb:603:in `async_exec'

Update: Getting the same error on Photo.all_tags.
Update: Updated to 0.6.0, same issue.

@antonysastre antonysastre changed the title Invalid query when running class methods? Invalid query when running some class methods? Jan 28, 2020
@antonysastre
Copy link
Author

The table for photos:

  create_table "photos", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.text "tags", default: [], array: true
    t.string "title"
    t.bigint "shoot_id"
    t.integer "creative_id"
    t.index ["creative_id"], name: "index_photos_on_creative_id"
    t.index ["shoot_id"], name: "index_photos_on_shoot_id"
    t.index ["tags"], name: "index_photos_on_tags", using: :gin
  end

@antonysastre
Copy link
Author

Having a hard time fixing this, anyone else experiencing this problem?
Is ActiveRecord generating a broken SQL-query? @skatkov @vfonic

@vfonic
Copy link
Contributor

vfonic commented Jan 29, 2020

@antonysastre for me everything works. The method I'm calling is #tags_cloud, instead of #tag_cloud.

acts-as-taggable-array-on (0.6.0)

  create_table "events", force: :cascade do |t|
    # ...
    t.string   "tags", :default=>[], :null=>false, :array=>true
  end

@antonysastre
Copy link
Author

antonysastre commented Jan 30, 2020

Thanks @vfonic i actually misspelled the method call. I was calling Photo.tags_cloud not Photo.tag_cloud.

I updated the issue. 🙂

Update: Getting the same error on Photo.all_tags.

@vfonic
Copy link
Contributor

vfonic commented Jan 30, 2020

I just went back to my project and realized I'm not using acts_as_taggable_array_on :tags, but taggable_array :tags.

Perhaps that's why you're seeing the issue.

Please double check the README and follow the steps from there from beginning.

@antonysastre
Copy link
Author

@vfonic Good catch! I updated to 0.6.0 and the new alias taggable_array but no luck :/
The problem seems to be in the SQL query, i'll keep digging.

@antonysastre
Copy link
Author

@vfonic What DB are you using? MySQL, PostreSQL or other?

@skatkov
Copy link
Collaborator

skatkov commented Jan 30, 2020

@antonysastre I remember seeing this issue in my codebase in the past, but don't remember how I've dealt with it. Would love to fix it as well.

It would be really helpful, if you can reproduce this issue in a test.

I've been successfully using temping gem ( https://github.com/jpignata/temping ) in the past to model database-backed AR relationship to test similar issues.

@antonysastre
Copy link
Author

antonysastre commented Jan 31, 2020

@skatkov Great to know thanks, i'm working on recreating it in the tests. I can confirm that it has to do with the construction of the SQL query.

SELECT tag, count(*) as count FROM (SELECT unnest(photos.tags) as tag FROM "photos") subquery GROUP BY tag ORDER BY photos.created_at DESC, tag

Doesn't work since the photos.created_at param is not accessible in the result rows (tag, count).

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

3 participants