【问题标题】:Rails: Getting "Table events has no column named user_id" error on all testsRails:在所有测试中出现“表事件没有名为 user_id 的列”错误
【发布时间】:2014-04-02 04:08:57
【问题描述】:

我所有的测试都出错了。我试着整理了好一阵子,无济于事。它们类似于以下内容:

30) Error:                                                                                                     
UserTest#test_a_user_should_enter_a_first_name:                                                                 
ActiveRecord::StatementInvalid: SQLite3::SQLException: table events has no column named user_id: INSERT INTO "ev
ents" ("text", "event_start", "user_id", "created_at", "updated_at", "id") VALUES ('MyText', '2012-08-10', 20790
7133, '2014-04-02 03:43:47', '2014-04-02 03:43:47', 980190962)                                                  


 31) Error:                                                                                                     
UserTest#test_a_user_should_enter_a_last_name:                                                                  
ActiveRecord::StatementInvalid: SQLite3::SQLException: table events has no column named user_id: INSERT INTO "ev
ents" ("text", "event_start", "user_id", "created_at", "updated_at", "id") VALUES ('MyText', '2012-08-10', 20790
7133, '2014-04-02 03:43:47', '2014-04-02 03:43:47', 980190962)                                                  


 32) Error:                                                                                                     
UserTest#test_a_user_should_have_a_profile_name_without_spaces:                                                 
ActiveRecord::StatementInvalid: SQLite3::SQLException: table events has no column named user_id: INSERT INTO "ev
ents" ("text", "event_start", "user_id", "created_at", "updated_at", "id") VALUES ('MyText', '2012-08-10', 20790
7133, '2014-04-02 03:43:47', '2014-04-02 03:43:47', 980190962) 

我应该提一下,该应用程序本身运行良好。各种东西都依赖于 user_id 属性,它们会运行。

至于测试,我环顾四周,发现提到了Check your fixtures file! Whenever I'd rename columns, I'd usually forget to tweak my fixtures to match. This causes an error on every single test, just like you're seeing. Error in all unit and functional tests after altering column names of a table 不确定这是否适用于我的问题。如您所见,它表示事件已经有一个 user_id。我不太明白所有测试是如何失败的,即使是看似无关的测试。这就是让我失望的原因。

我的 GitHub 板块:https://github.com/attatae/and_silva

还看了这个:Rails 3.0.7 with ruby 1.9.2 fixtures with :belongs_to and :has_many gives "table users has no column named posts"

我需要将 user_id 插入 xxyynum_create_events.rb 吗? ie integer :user_idcreate table :events do |t|

我尝试过的其他事情: 耙分贝:测试:准备 rake db:migrate RAILS_ENV=test 删除然后重新创建数据库

非常感谢所有帮助。

更新================

发布后,我将 [bignum]_create_events.rb 更改为如下所示:

class CreateEvents < ActiveRecord::Migration
  def change
    create_table :events do |t|
      t.string :title
      t.datetime :time_begin 
      t.string :location
      t.date :event_start
      t.text :text
      t.integer :user_id
      t.text :user 

      t.timestamps
    end
  end
end

然后我尝试做bundle exec rake db:reset &amp;&amp; bundle exec rake db:populate &amp;&amp; bundle exec rake db:test:prepare,但收到错误“不知道如何构建任务'db:populate'。

所以我尝试了bundle exec rake db:drop &amp;&amp; bundle exec rake db:create &amp;&amp; bundle exec rake db:migrate &amp;&amp; bundle exec db:test:prepare,但它说 no such table: events: ALTER TABLE "events" ADD "address" varchar(255)C:/Rails Projects/Git pull/and_silva/db/migrate/20140329045140_add_address_to_event.rb:3:inchange'`

接下来我进入了开发和测试沙箱,他们都说Event(Table doesn't exist)

然后我尝试运行 rails s 只是为了确保我什至尝试修复正确的程序,因为我的应用程序之前运行得很好,显然事件中没有 user_id 方面。

该网站说Migrations are pending; run 'rake db:migrate RAILS_ENV=development' to resolve this issue. 我尝试运行 rake db:migrate 代替。然后说:

==  AddAddressToEvent: migrating ==============================================
-- add_column(:events, :address, :string)
rake aborted!
An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: no such table: events: ALTER TABLE "events" ADD "address" varchar(255)C:/Rails Projects/Git pull/and_silva/db/migrate/20140329045140_add_address_to_event.rb:3:in *change'
C:in `migrate'
Tasks: TOP => db:migrate

然后我运行 rake 并在 32 次测试中得到与以前相同的错误。

我运行了 rake db:migrate RAILS_ENV=development,然后得到了相同的 AQLite3 [...] ALTER TABLE 错误。

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    检查灯具,看看它们是否与您的架构不匹配。

    我遇到了类似的问题。即使我将其从数据库中删除,我的固定装置也具有旧值。因此,Rails 测试正在加载固定装置,它们与我更新的数据库不同。

    【讨论】:

      【解决方案2】:

      只需输入rake db:test:prepare即可。

      【讨论】:

        【解决方案3】:

        查看您的 /db/* 架构和迁移文件,我无法在任何地方看到为事件表定义的 user_id。

        我发现它是heredb/schema.rb

        修复它并尝试: bundle exec rake db:reset &amp;&amp; bundle exec rake db:populate &amp;&amp; bundle exec rake db:test:prepare

        如果这还不够,您应该会收到错误消息,指出需要其他一些 rake 任务。

        【讨论】:

        • 抱歉,我是 Rails 新手,不太了解这个建议。如果您确实找到了它,测试仍然不会运行。上面显示的错误来自我将 user_id 添加到 create_events 迁移。当我编辑我的主要问题以反映时,应用程序运行,因此除此之外,还不清楚在哪里进行更改。您的意思是添加 t.integer :user_id 到 create_events 迁移?
        • 我在你的 github repo 上检查了它,在当前版本中没有 user_id @events 表。我发现它是以前的一些。检查上面的链接以查看您所做更改的差异。如果您的应用程序运行良好并且测试不是因为您的数据库版本用于测试和开发。不一样。
        • 您可以手动检查那里有什么。在两者上运行 rails c test --sandboxrails c dev --sandbox 并输入 Event 并进行比较。
        • 嗨 pawel7318,我照你说的做了。但是,我的应用程序说“不知道如何构建任务 'db:populate”。还有其他选择吗?我得到其他错误。显然,我的应用开发和测试区域没有事件表。
        • db:populate 用于填充您的数据库。带有样本数据。如果你没有它,那么你就不需要它。如果您愿意,我们可以移动聊天。只需创建一个频道并邀请我到那里。
        【解决方案4】:

        在我的情况下,我的 ActiveRecord 中有一个 enum 值(所以我的表中只有一个整数),然后我决定将其更改为 reference。删除列并添加相应的引用后,我得到了测试错误。我的错误是我忘记在添加了引用的模型中添加belongs_to :MODEL

        【讨论】:

          猜你喜欢
          • 2022-01-05
          • 2020-06-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-06-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多