sql - 如何在 create_table 上设置初始 id 值?

标签 sql ruby-on-rails

我想让 create_table rails 命令的自动递增 id 列从 500 开始。

我找到了如何在 sql 中设置初始 auto_increment id 值的示例:

CREATE TABLE student ( id int(2) NOT NULL auto_increment, name varchar(50) NOT NULL default '', class varchar(10) NOT NULL default '', mark int(3) NOT NULL default '0', sex varchar(6) NOT NULL default 'male', UNIQUE KEY id (id) ) auto_increment=100000 TYPE=MyISAM; 

所以我查看了 Rails API,并在 create_table 方法中看到了这些选项:

The options hash can include the following keys:

:id
    Whether to automatically add a primary key column. Defaults to true. Join tables for has_and_belongs_to_many should set :id => false. 
:primary_key
    The name of the primary key, if one is to be added automatically. Defaults to id. 
:options
    Any extra options you want appended to the table definition. 
:temporary
    Make a temporary table. 
:force
    Set to true to drop the table before creating it. Defaults to false. 

不是我需要的...我尝试过但没有成功:

:options => {:auto_increment => 500}

任何人都知道如何让此语句的自动递增 id 列从 500 开始:

create_table :contents do |t|
  t.string  :type,                :null => false
  t.string  :name,                :null => false
end

最佳答案

试试这个:

create_table(:student, :options => 'AUTO_INCREMENT = 500') do |t|
  t.column :name, :string, :limit => 50
  # Other fields here   
end

关于sql - 如何在 create_table 上设置初始 id 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/784007/

相关文章:

mysql - 将 bool 列转换为整数

ruby-on-rails - bundler /设置(LoadError)

html - 将我的 html-css 设计整合到 ruby​​ on rails 中

ruby-on-rails - 测试 Redcarpet Markdown 助手

MySQL 返回的值与提供的示例代码中的最大值不同

java - 更新语句语法错误

python - 通过复制记录创建大型数据集

sql - 在 SQL (SQLite3) 中使用 IN 和元组集

ruby-on-rails - rails : How to conditionally apply ActiveRecord sorting by one attribute based on through association or other property

ruby-on-rails - RoR、Ajax、可排序、序列化