How do I create an UNSIGNED INT for MySQL in a Rails 5 migration?

Viewed 2792

With activerecord (< 5.0, >= 3.2) I was able to use the activerecord-mysql-unsigned gem to create an UNSIGNED INT in my MySQL database, but there's been no updates to that gem and I can't find any documentation regarding native support for that in Rails 5.

Is there an options hash or something that can be called in the add_column method which allows this to be done?

2 Answers

If are using rails > 5.1 and you want to have all your primary keys being generated with unsigned integer as default type, all that you have to do is adapt your generator in your config/application.rb , adding the following code:

config.generators do |g|
      # ... other configurations
      g.orm :active_record, primary_key_type: :unsigned_integer
end

With that a call to rails generate model foo bar:string will create the id as unsigned.

Related