ActiveRecord gives SQL "no such column" error for simple has_many association

It drives me crazy! This code worked fine, but a few weeks ago it stopped working, and I can't figure out why. Basically, the game has a lot of patches. The error occurs in my PatchesController, but its rails reproduced in the console looks like this:

first_game = Game.find(:first)
first_game.patches

As soon as I use the patches method, I get the following:

ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: patches.game_true: SELECT * FROM "patches" WHERE ("patches".game_true = 1) 
    from /project_root/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:221:in `rescue in log'
    from /project_root/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:204:in `log'
    from /project_root/vendor/rails/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:172:in `block in execute'
    from /project_root/vendor/rails/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:417:in `catch_schema_changes'
    from /project_root/vendor/rails/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:172:in `execute'
    from /project_root/vendor/rails/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:320:in `select'
    from /project_root/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all'
    from /project_root/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:62:in `select_all_with_query_cache'
    from /project_root/vendor/rails/activerecord/lib/active_record/base.rb:664:in `find_by_sql'
    from /project_root/vendor/rails/activerecord/lib/active_record/base.rb:1578:in `find_every'
    from /project_root/vendor/rails/activerecord/lib/active_record/base.rb:618:in `find'
    from /project_root/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:60:in `find'
    from /project_root/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:400:in `find_target'
    from /project_root/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:354:in `load_target'
    from /project_root/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:140:in `inspect'
    from /usr/local/bin/irb:12:in `<main>'

Now that SQL should really say "WHERE patches.game_id = 1" if I don't lose my mind. I have no idea why it generates this SQL!

Here's the /game.rb model:

class Game < ActiveRecord::Base
  has_many :patches
end

Here's the /patches.rb model:

class Patch < ActiveRecord::Base
  belongs_to :game
end

"game_id" 3 , . my_patch.game, Game, , . !

+3
3

. . .

def up
create_table :reviews do |t|
    t.integer       :potatoes
    t.text          :comments
    t.references    :moviegoer
    t.references    :movie
end

. , rake db: drop rake db: migrate

+3

, - . , Game :

class Game < ActiveRecord::Base
  # Form #1
  self.primary_key = true

  # Form #2
  set_primary_key true
end

"id" , "".

0

ummm is a dumb question, but doesn't this feedback claim that the missing column is called "game_true" (in the patch table)? I do not think he claims that there is no column called “patches”.

SQLException: no such column: patches.game_true: SELECT * FROM "patches" WHERE ("patches".game_true = 1)

This should change when you need to look.

0
source

Source: https://habr.com/ru/post/1755313/


All Articles