Rails:在所有测试中获取"表事件没有名为user_id的列"错误

Cod*_*rus 6 ruby-on-rails

我所有的测试都给出了错误.我尝试排序好一段时间,但无济于事.它们类似于以下内容:

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) 
Run Code Online (Sandbox Code Playgroud)

我应该提到应用程序本身工作得非常好.各种各样的东西取决于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.不确定这是否适用于我的问题.如您所见,它表示事件已经有一个user_id.我不太了解所有测试都失败了,即使是看似无关的测试.这就是让我失望的原因.

我的GitHub部分:https://github.com/attatae/and_silva

另外看了一下:Rails 3.0.7与ruby 1.9.2 fixtures:belongs_to和:has_many给出"表用户没有列名为帖子"

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

我试过的其他事情:rake db:test:准备rake db:migrate RAILS_ENV = test drop然后重新创建db

所有帮助非常感谢.

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

发布后,我将[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
Run Code Online (Sandbox Code Playgroud)

然后我尝试了bundle exec rake db:reset && bundle exec rake db:populate && bundle exec rake db:test:prepare但收到错误"不知道如何构建任务'db:populate'.

所以我尝试了bundle exec rake db:drop && bundle exec rake db:create && bundle exec rake db:migrate && 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:in改变'`

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

然后我尝试运行rails只是为了确保我甚至试图修复正确的程序,因为我的应用程序之前运行得很好,显然在事件中没有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
Run Code Online (Sandbox Code Playgroud)

然后我跑了rake并得到了与之前32次测试相同的错误.

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

Dav*_*com 8

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

我遇到了类似的问题.我的灯具有一个旧值,即使我从我的数据库中删除它.因此,Rails测试正在加载灯具,它们与我更新的数据库不同.


paw*_*318 0

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

我发现它就在这里db/schema.rb

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

如果这还不够,您应该会收到需要rake执行其他任务的错误消息。