2013-03-07 133 views
0

我正在阅读RoR的在线指南,并看到了这段迁移代码。什么是迁移索引?

change_table: products do |t| 
    #some code here 

    t.string :part_number 
    t.index :part_number 

    #some code here 
end 

这是什么指标?由于我已经有一个名为part_number的字符串列,为什么可以添加另一个具有相同名称但类型不同的列?

+0

对于SQL数据源,它映射到CREATE INDEX语句([MySQL](http://dev.mysql.com/doc/refman/5.6/en/create-index.html),[Postgres]( http://www.postgresql.org/docs/9.2/static/sql-createindex.html))。这会创建一个名为':part_number'的字符串列,然后创建一个索引列来加速查询。 – 2013-03-07 21:19:52

+0

如果我记得没错,索引的列名实际上是'part_number_index' – AdamT 2013-03-07 21:35:55

回答

2

这就是说,part_number将被用作索引。该列的名称不是part_number

+0

索引是否与主键相同?那么这个专栏的名字是什么? – OneZero 2013-03-07 21:12:51

+0

不,索引是组织数据库的一种方式,因此很容易搜索。 – AdamT 2013-03-07 21:14:14

+0

所以一张桌子只能有一个索引,对吧? – OneZero 2013-03-07 21:15:08