如何在rails表定义中除'id'之外的另一列添加'auto increment'?

时间:2010-08-13 03:05:30

标签: ruby-on-rails ruby auto-increment

如果我需要将'auto_increment'添加到表格中名为'another_id'的新列中,我该如何制作呢?
有没有像:

这样的选项
create_table :posts do |t|
  t.integer :another_id, :auto_increment => true # Is there a option like this?
  ...

  t.timestamps
end

在开发环境中,我在生产环境中使用sqlite3和mysql;

1 个答案:

答案 0 :(得分:0)

我不相信create_table的rails api有自动增量选项。在create_table之后的up方法中,您可能必须执行ALTER TABLE sql命令将列设置为自动增量。请注意,对于数据库供应商而言,这可能会有所不同。

语法类似于:执行“ALTER TABLE tbl_name MODIFY col_name AUTO INCREMENT”

或者你可以在up方法中运行execute命令来使用sql create table语句创建表。