Search code examples
ruby-on-railssqliteherokupg

Heroku pg, can not migrate int with limit 19


I have a database column as integet set to limit 19. It works fine on localhost as I have sqlite. But I can not migrate on Heroku pg database.

class AdddivIdToLocations < ActiveRecord::Migration
  def change
    add_column :locations, :div_id, :integer, :limit => 19
  end
end

Then I tried to change the column from integer to string which would be even better. But because it can not proceed the heroku run rake db:migrate I can not change the column type. What should I do ?

class AdddivIdToLocationsTypeChange < ActiveRecord::Migration

  def self.up
    change_table :locations do |t|
      t.change :div_id, :string
    end
  end
  def self.down
    change_table :locations do |t|
      t.change :div_id, :integer
    end
  end
end

Solution

  • What's the maximum value div_id can have?

    :limit specifies what's the maximum storage size in bytes - Available values are follow:

    +------------------------------------------------------------+
    | :limit | Numeric Type  | Column Size |      Max Value      |
    |--------+---------------+-------------+---------------------|
    |    1   |    TINYINT    |   1 byte    | 127                 |
    |    2   |    SMALLINT   |   2 bytes   | 32767               |
    |    3   |    MEDIUMINT  |   3 bytes   | 8388607             |
    |    4   |     INT(11)   |   4 bytes   | 2147483647          |
    |    8   |     BIGINT    |   8 bytes   | 9223372036854775807 |
    +------------------------------------------------------------+
    

    If you don't specify any value then by default it's 4 INT(11). If your value would be large than an integer then set limit: 8 so it can store BIGINT. 19 isn't a valid value.

    You can update limit: part of the migration and run the db:migrate again. If you want to change the column type then first this column has to exist, for this run db:migrate without limit: