12

How could I make population unsigned?

  def self.up
    create_table :cities do |t|
      t.string :name
      t.integer :population
      t.float :latitude
      t.float :longitude

      t.timestamps
    end
  end
tybro0103
  • 48,327
  • 33
  • 144
  • 170

3 Answers3

31

This should work for you.

t.column :population, 'integer unsigned'
rwilliams
  • 21,188
  • 6
  • 49
  • 55
7

Just to add one more thing. This works perfectly. But need to note that it will make the migration code database dependent.

Indika K
  • 1,332
  • 17
  • 27
2

step 1:

add activerecord-mysql-unsigned to GemFile

# add unsigned integer support to mysql2 adapter
gem "activerecord-mysql-unsigned", "~> 0.0.1"

step 2: install gems

bundle install

step 3:

use "unsigned: true" in fields you like

t.integer :cost, unsigned: true

refrence : http://rubydoc.info/gems/activerecord-mysql-unsigned/0.0.1/frames

Mohsen Alizadeh
  • 1,595
  • 12
  • 25
  • 5
    Be VERY careful with this gem if you're working within an existing project. This will _override_ Rails' definition of what a primary_key should be to make it unsigned! see here -> https://github.com/waka/activerecord-mysql-unsigned/blob/v0.2.0/lib/activerecord-mysql-unsigned/active_record/v3/connection_adapters/abstract_mysql_adapter.rb#L7-L9 – Mike Lewis Oct 03 '14 at 20:42