gpt4 book ai didi

ruby-on-rails - ActiveRecord 为简单的 has_many 关联给出 'no such column' SQL 错误

转载 作者:行者123 更新时间:2023-12-04 06:08:58 25 4
gpt4 key购买 nike

这让我发疯!这段代码过去工作正常,但几周前它停止工作了,我不明白为什么。基本上一个游戏有很多补丁。错误发生在我的 PatchesController 中,但它可以在 Rails 控制台中重现,如下所示:

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

只要我使用补丁方法,我就会得到这个:

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>'

现在 SQL 真的应该说“WHERE patches.game_id = 1”,除非我要发疯了。我不知道它为什么会生成那个 SQL!

这是 models/game.rb:

class Game < ActiveRecord::Base
has_many :patches
end

这是 models/patches.rb:

class Patch < ActiveRecord::Base
belongs_to :game
end

patches 表中有'game_id' 和 3 个条目,都是第一个游戏。如果我得到其中一个补丁并转到 my_patch.game,它会毫无问题地返回它所属的 Game 对象。任何帮助将不胜感激!

最佳答案

我能够找出问题所在。它在我的迁移文件中。我在我的迁移文件中使用了引用助手。

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

我拼错了父类的模型名称。更正名称,然后删除我的数据库并重新创建它 rake 分贝:下降 rake 数据库:迁移

关于ruby-on-rails - ActiveRecord 为简单的 has_many 关联给出 'no such column' SQL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3281363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com