小编Jay*_*ock的帖子

rails生成迁移add_index_to ...不会将实际索引放在迁移文件中

我通过"rails generate model User name:string email:string ..."创建了一个用户表,同时创建了迁移文件.

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name
      t.string :email

      t.timestamps
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

现在我想在"跟随教程"的电子邮件列中添加一个索引.我第一次成功地使用sqlite3完成了这项工作.第二次通过im使用MySql(mysql2).再次使用生成模型创建表格.当我运行以下内容时:

rails generate migration add_index_to_users_email
Run Code Online (Sandbox Code Playgroud)

该过程以没有错误消息结束并创建迁移文件,如下所示,但没有任何索引的设置..

class AddIndexToUsersEmail < ActiveRecord::Migration
  def change
  end
end
Run Code Online (Sandbox Code Playgroud)

我希望add_index :users, :email, unique: true在那里看到 ...任何人有任何想法..搜索其他线程无济于事.运行rails 4,mysql 5.6 ruby​​ 1.9.3我在initil db:migrate之后创建的架构是:

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

  create_table "users", force: true do |t|
    t.string   "name"
    t.string   "email"
    t.string   "city"
    t.string   "state"
    t.string   "zip"
    t.string   "mobile_phone"
    t.string   "mobile_phone_type"
    t.date     "birth_date"
    t.string   "user_type"
    t.string …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails

18
推荐指数
2
解决办法
3万
查看次数

标签 统计

ruby-on-rails ×1