Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

[BUG] delete_all using wrong database connection when executing on relation

#6078

Context

Using Rails 3.0.3 and Ruby 1.9.2

In one rails project i'm using multiple database connections (code below) and different adapters (mysql and pq).
Connection to RedMine using Mysql adapter
Connection to main database using PostgreSQL adapter

Problem

When I fetching all specification for project it works fine


ruby-1.9.2-p0 > project.specifications
 => [#<RedMine::SpecificationCache id: 1, project_id: 45, version_id: 71, developer_id: nil, status: "@_todo", estimation: nil, tags: "@mapa @__kb_articles", scenario_title: "Scenario 1", feature_title: "Feature 1", feature_hash_id: nil>, #<RedMine::SpecificationCache id: 3, project_id: 45, version_id: 71, developer_id: nil, status: "@_backlog", estimation: nil, tags: "@map @__kb_articles", scenario_title: "Scenario 1", feature_title: "Feature 2", feature_hash_id: nil>, #<RedMine::SpecificationCache id: 4, project_id: 45, version_id: 71, developer_id: 0, status: "@_done", estimation: nil, tags: "@__kb_articles @test @map @onet", scenario_title: "Scenario 2", feature_title: "Feature 1", feature_hash_id: nil>]

but when i try to delete all specification that is related to particular project, sql is executed on main database instead of using connection to RedMine database. ( and if this could be big problem in production - of course in some circumstances )


ruby-1.9.2-p0 > project.specifications.delete_all
ActiveRecord::StatementInvalid: PGError: ERROR:  relation "specification_cache" does not exist
LINE 4:              WHERE a.attrelid = '"specification_cache"'::reg...
                                        ^
:             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
              FROM pg_attribute a LEFT JOIN pg_attrdef d
                ON a.attrelid = d.adrelid AND a.attnum = d.adnum
             WHERE a.attrelid = '"specification_cache"'::regclass
               AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum

    from /home/mc/.rvm/gems/ruby-1.9.2-p0@selleo-dashboard/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract_adapter.rb:202:in `rescue in log'
    from /home/mc/.rvm/gems/ruby-1.9.2-p0@selleo-dashboard/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract_adapter.rb:194:in `log'
    from /home/mc/.rvm/gems/ruby-1.9.2-p0@selleo-dashboard/gems/activerecord-3.0.3/lib/active_record/connection_adapters/postgresql_adapter.rb:483:in `query'
    from /home/mc/.rvm/gems/ruby-1.9.2-p0@selleo-dashboard/gems/activerecord-3.0.3/lib/active_record/connection_adapters/postgresql_adapter.rb:1015:in `column_definitions'
    from /home/mc/.rvm/gems/ruby-1.9.2-p0@selleo-dashboard/gems/activerecord-3.0.3/lib/active_record/connection_adapters/postgresql_adapter.rb:662:in `columns'
    from /home/mc/.rvm/gems/ruby-1.9.2-p0@selleo-dashboard/gems/arel-2.0.4/lib/arel/table.rb:93:in `columns'
    from /home/mc/.rvm/gems/ruby-1.9.2-p0@selleo-dashboard/gems/arel-2.0.4/lib/arel/table.rb:100:in `[]'
    from /home/mc/.rvm/gems/ruby-1.9.2-p0@selleo-dashboard/gems/activerecord-3.0.3/lib/active_record/associations/has_many_association.rb:78:in `delete_records'
    from /home/mc/.rvm/gems/ruby-1.9.2-p0@selleo-dashboard/gems/activerecord-3.0.3/lib/active_record/associations/association_collection.rb:222:in `block in delete'
    from /home/mc/.rvm/gems/ruby-1.9.2-p0@selleo-dashboard/gems/activerecord-3.0.3/lib/active_record/associations/association_collection.rb:525:in `block in remove_records'
    from /home/mc/.rvm/gems/ruby-1.9.2-p0@selleo-dashboard/gems/activerecord-3.0.3/lib/active_record/associations/association_collection.rb:158:in `block in transaction'
    from /home/mc/.rvm/gems/ruby-1.9.2-p0@selleo-dashboard/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
    from /home/mc/.rvm/gems/ruby-1.9.2-p0@selleo-dashboard/gems/activerecord-3.0.3/lib/active_record/transactions.rb:204:in `transaction'
    from /home/mc/.rvm/gems/ruby-1.9.2-p0@selleo-dashboard/gems/activerecord-3.0.3/lib/active_record/associations/association_collection.rb:157:in `transaction'
    from /home/mc/.rvm/gems/ruby-1.9.2-p0@selleo-dashboard/gems/activerecord-3.0.3/lib/active_record/associations/association_collection.rb:522:in `remove_records'
    from /home/mc/.rvm/gems/ruby-1.9.2-p0@selleo-dashboard/gems/activerecord-3.0.3/lib/active_record/associations/association_collection.rb:221:in `delete'
    from /home/mc/.rvm/gems/ruby-1.9.2-p0@selleo-dashboard/gems/activerecord-3.0.3/lib/active_record/associations/association_collection.rb:167:in `delete_all'
    from (irb):7
    from /home/mc/.rvm/gems/ruby-1.9.2-p0@selleo-dashboard/gems/railties-3.0.3/lib/rails/commands/console.rb:44:in `start'
    from /home/mc/.rvm/gems/ruby-1.9.2-p0@selleo-dashboard/gems/railties-3.0.3/lib/rails/commands/console.rb:8:in `start'
    from /home/mc/.rvm/gems/ruby-1.9.2-p0@selleo-dashboard/gems/railties-3.0.3/lib/rails/commands.rb:23:in `<top (required)>'
    from script/rails:6:in `require'

I'm not sure but maybe AR is setting wrong engine on Arel, but not able to confirm or deny this

Source Code


class Abstract::RedMine < ActiveRecord::Base
  self.abstract_class = true
  establish_connection("redmine_#{Rails.env}")

  private

  def self.instantiate(record)
    (record[self.inheritance_column] = "RedMine::"+record[self.inheritance_column]) if record[self.inheritance_column]
    super
  end
end

class RedMine::Project < Abstract::RedMine
  # ...
  has_many :specifications, :class_name => 'RedMine::SpecificationCache'
  # ...
end

class RedMine::SpecificationCache < Abstract::RedMine
  set_table_name 'specification_cache'
end

Reported by michalczyz (at gmail) · November 28th, 2010 @ 01:53 PM

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. rails
    rails
    • Tag changed from 3.0.3 active_record arel to 303 active_record arel
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    March 1st, 2011 @ 12:00 AM

  2. rails
    rails
    • State changed from open to stale

    March 1st, 2011 @ 12:00 AM