使用activerecord (< 5.0, >= 3.2)
我可以使用activerecord-mysql-unsigned
gem在我的MySQL数据库中创建一个UNSIGNED INT,但是没有对该gem的更新,我找不到任何有关本机支持的文档在Rails 5中。
是否有一个选项哈希或者可以在add_column方法中调用的东西允许这样做?
答案 0 :(得分:2)
Rails 5.1.x中有无符号整数,bigint,decimal和float in the schema adapter for MySQL的选项
迁移中的类似内容将适用于Rails 5.1.4
def up
create_table :unsigned_columns do |t|
t.integer "positive", :unsigned => true
t.timestamps
end
end
答案 1 :(得分:0)
如果使用 rails > 5.1 并且您希望使用 unsigned integer
作为默认类型生成所有主键,那么您所要做的就是在 config/application.rb
中调整您的生成器,添加以下代码:
config.generators do |g|
# ... other configurations
g.orm :active_record, primary_key_type: :unsigned_integer
end
这样调用 rails generate model foo bar:string
会将 id
创建为未签名。