Ruby on rails,测试是说列不存在,但它在架构上

时间:2016-12-31 15:28:54

标签: mysql ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2

在我的客户表中,我有一个名为email的列。但是,当我为客户端控制器和模型进行测试时,测试继续说客户端表没有名为email的列。

SQLite3 :: SQLException:表客户端没有名为email的列:CREATE UNIQUE INDEX" index_clients_on_email" ON"客户" ("电子邮件&#34)

虽然我承认在创建我的表时我最初没有放过该列,但是我通过单独的迁移添加了该列。我运行rake db:migrate甚至尝试了rake db:drop:all,rake db:create:all然后rake db:migrate,它仍然没有改变任何东西。

电子邮件列也被添加为clients表的索引。

这是我的架构:

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

客户端表的初始迁移:

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

迁移以添加电子邮件作为客户端表的索引:

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

迁移以添加电子邮件列:

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

以下是我的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

1 个答案:

答案 0 :(得分:1)

尝试:

RAILS_ENV=test bundle exec rake db:schema:load