class CreateSimpleModels < ActiveRecord::Migration
def self.up
create_table :simple_models do |t|
t.string :name
t.integer :user_number
t.timestamps
end
execute "CREATE SEQUENCE simple_models_user_number_seq OWNED BY
simple_models.user_number INCREMENT BY 1 START WITH 1"
end
def self.down
drop_table :simple_models
execute "DELETE SEQUENCE simple_models_user_number_seq"
end
end
3条答案
按热度按时间tvz2xvvm1#
pkwftd7m2#
你可以直接修改你的mysql表,试试这个:
Alter table Tablename modify column ColumnName int(11) auto_increment;
您可以引用Auto increment a non-primary key field in Ruby on Rails以rails方式执行它。
你也可以想出一个非常愚蠢的方法,比如在你的模型中,在create方法中:
但是我仍然会问为什么不使用
id
本身作为user_number
?e0uiprwp3#
对于Rails 6 +