如何创建一个新表具有唯一索引中的Active Record / Rails的4迁移 [英] How to Create a New Table With a Unique Index in an Active Record / Rails 4 Migration

查看:91
本文介绍了如何创建一个新表具有唯一索引中的Active Record / Rails的4迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何创建一个新表,通过轨道迁移,并添加一个唯一索引呢?

How do I create a new table, through a rails migration, and add an unique index to it?

在文档,我发现了如何将索引添加到表它创建后年代,但你如何做到既 - 创建表,并添加唯一索引 - 在同一个迁移文件

In the docs I found how to add a index to a table after it's been created, but how do you do both -- create the table, and add the unique index -- in the same migration file?

推荐答案

以下是完整过程:

生成迁移(导轨产生迁移CreateFoos吧:字符串

修改迁移看起来是这样的:

Modify your migration to look something like this:

class CreateFoos < ActiveRecord::Migration
  def change
    create_table :foos do |t|
      t.string :bar, :null => false

      t.index :bar, unique: true
    end
  end
end

运行耙分贝:迁移

这篇关于如何创建一个新表具有唯一索引中的Active Record / Rails的4迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆