Ruby-on-rails rails 生成迁移 add_index_to... 不会将实际索引放入迁移文件中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19572875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:56:00  来源:igfitidea点击:

rails generate migration add_index_to... does not put the actual index in the migration file

ruby-on-rails

提问by JaySchrock

I created a users table via "rails generate model User name:string email:string ..." the migration file was created as well.

我通过“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

Now I want to add an index to the email column "following the tutorial" I've done this successfully the first time through using sqlite3. Second time through im using MySql (mysql2). Again created the table fine with generate model.. When I run the following:

现在我想在“遵循教程”的电子邮件列中添加一个索引,我第一次通过使用 sqlite3 成功地完成了此操作。第二次通过我使用 MySql (mysql2)。再次使用生成模型创建了表格。当我运行以下命令时:

rails generate migration add_index_to_users_email

the process ends with no error message and creates the migration file as shown below, but there is no setting of any index..

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

class AddIndexToUsersEmail < ActiveRecord::Migration
  def change
  end
end

Im expecting to see add_index :users, :email, unique: truein there ... Anybody have any idea's.. searched other threads to no avail.. running rails 4, mysql 5.6 ruby 1.9.3 my schema that was created after initil db:migrate is:

我期待add_index :users, :email, unique: true在那里看到 ......任何人都有任何想法......搜索其他线程无济于事......运行 rails 4, mysql 5.6 ruby​​ 1.9.3 我在 intil 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   "ss_num"
    t.boolean  "agree_to_terms"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end

回答by blotto

via http://guides.rubyonrails.org/migrations.html

通过http://guides.rubyonrails.org/migrations.html

If you'd like to add an index on the new column, you can do that as well:

$ rails generate migration AddPartNumberToProducts part_number:string:index

如果您想在新列上添加索引,您也可以这样做:

$ rails 生成迁移 AddPartNumberToProducts part_number:string:index

your generator

你的发电机

rails generate migration add_index_to_users_email

simply creates an empty migration file and did not describe a index

只是创建了一个空的迁移文件,并没有描述索引

so this would be more appropriate...

所以这会更合适......

rails generate migration AddIndexToUsers email:string:index

should give you

应该给你

class AddIndexToUsers < ActiveRecord::Migration
  def change
    add_index :users, :email
  end
end


Nguyen You - EDIT

阮友 - 编辑

This command [Rails 5.2.3]

此命令 [Rails 5.2.3]

rails generate migration AddIndexToUsers email:string:index

actually will give you

实际上会给你

class AddIndexToUsers < ActiveRecord::Migration[5.2]
  def change
    add_column :users, :email, :string
    add_index :users, :email
  end
end

not only add_indexbut also add_columnto the users table.

不仅如此,用户表add_index也是如此add_column

回答by Oleg Sobchuk

rails generate migration AddIndexToUsers email:string:index

rails generate migration AddIndexToUsers email:string:index

if you already have column it just add index, like:

如果您已经有列,则只需添加索引,例如:

class AddIndexToUsers < ActiveRecord::Migration
  def change
    add_index :users, :email
  end
end

if you create new column (you haven't got column in database yet), it returns:

如果您创建新列(您还没有在数据库中获得列),它将返回:

class AddIndexToUsers < ActiveRecord::Migration
  def change
    add_column :user, :email, :string
    add_index :users, :email
  end
end

回答by RMazitov

From the http://railstutorial.ru/chapters/4_0/modeling-users#code-email_uniqueness_index.

来自http://railstutorial.ru/chapters/4_0/modeling-users#code-email_uniqueness_index

The email uniqueness migration is not pre-defined, so we need to fill in its contents with this by ourself " add_index :users, :email, unique: true " .

电子邮件唯一性迁移不是预先定义的,因此我们需要自己填写它的内容“ add_index :users, :email, unique: true ”。

The result will be:

结果将是:

class AddIndexToUsersEmail < ActiveRecord::Migration
  def change
    add_index :users, :email, unique: true
  end
end