Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Getting "Table events has no column named user_id" error on all tests

All my tests are giving errors. I tried to sort for a good while, to no avail. They are similar to the following:

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) 

I should mention that the app itself is working perfectly fine. Various things depend on the user_id attribute, and they run.

As for tests, I looked around and found a mention of 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 Not sure if that applies to my issue though. As you can see, it says events already has a user_id. I don't quite get how all the tests are failing, even seemingly unrelated ones. That's what's throwing me off.

My section of GitHub: https://github.com/attatae/and_silva

Also looked at this: Rails 3.0.7 with ruby 1.9.2 fixtures with :belongs_to and :has_many gives "table users has no column named posts"

Do I need to insert user_id into xxyynum_create_events.rb? ie integer :user_id under create table :events do |t|

Other things I tried: rake db:test:prepare rake db:migrate RAILS_ENV=test dropping then recreating the db

All help very appreciated.

Update===============

After posting, I changed my [bignum]_create_events.rb to look like so:

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

I then tried doing bundle exec rake db:reset && bundle exec rake db:populate && bundle exec rake db:test:prepare but received an error `"Don't know how to build task 'db:populate'.

So I tried bundle exec rake db:drop && bundle exec rake db:create && bundle exec rake db:migrate && bundle exec db:test:prepare instead, but it said 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'`

Next I went into dev and test sandboxes, and they both said that Event(Table doesn't exist).

Then I tried running rails s just to make sure I was even trying to fix the right program, since my application was running earlier just fine, apparently with no user_id aspect in events.

The site said Migrations are pending; run 'rake db:migrate RAILS_ENV=development' to resolve this issue. I tried running rake db:migrate instead. Then it said:

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

Then I ran rake and got the same exact errors as before for 32 tests.

I ran rake db:migrate RAILS_ENV=development and then got the same AQLite3 [...] ALTER TABLE error.

like image 242
CodeWalrus Avatar asked Apr 02 '14 04:04

CodeWalrus


2 Answers

Check fixtures and see if they are mis-matched to your schema.

I had a similar problem. My fixtures had an old value even though I removed it from my db. So, Rails test was loading fixtures and they were not the same as my updated db.

like image 160
DaveWoodall.com Avatar answered Nov 11 '22 15:11

DaveWoodall.com


Just type bundle exec rake db:test:prepare.It will work.

like image 4
Sajjad Murtaza Avatar answered Nov 11 '22 15:11

Sajjad Murtaza