简体   繁体   中英

Ruby on rails , test is saying a column doesn't exist but its on the schema

in my clients table, I have a column named email. But when I made the tests for the clients controller and the model, the tests kept on saying that the clients table has no column named email.

SQLite3::SQLException: table clients has no column named email: CREATE UNIQUE INDEX "index_clients_on_email" ON "clients" ("email")

although I do admit that I didn't initially put that column when I created my table, but I added the column via a separate migration. I ran rake db:migrate and even tried rake db:drop:all, rake db:create:all and then rake db:migrate and it still didn't change anything.

the email column was also added as an index for the clients table.

this is my schema:

ActiveRecord::Schema.define(version: 20161230163248) do

  create_table "clients", force: :cascade do |t|
    t.string   "name",       null: false
    t.text     "email",      null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  add_index "clients", ["email"], name: "index_clients_on_email", unique: true

  create_table "projects", force: :cascade do |t|
    t.text     "project_description", null: false
    t.string   "project_timescale"
    t.datetime "created_at",          null: false
    t.datetime "updated_at",          null: false
    t.integer  "client_id"
  end

  add_index "projects", ["client_id"], name: "index_projects_on_client_id"

end

the initial migration for the clients table:

class CreateClients < ActiveRecord::Migration
  def change
    create_table :clients do |t|
      t.string :name, presence: true, null: false
      t.timestamps null: false
    end
  end
end

migration to add email as an index for the client table:

class AddIndexToClient < ActiveRecord::Migration
  def change
    add_index:clients, :email, unique: true
  end
end

migration to add the email column:

class AddEmailToClient < ActiveRecord::Migration
  def change
    add_column :clients, :email, :text
  end
end

the following is my database.yml:

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default
  adapter: sqlite3
  pool: 5
  timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: db/test.sqlite3

production:
  <<: *default
  database: db/production.sqlite3

尝试:

RAILS_ENV=test bundle exec rake db:schema:load

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM