【问题标题】:Heroku pg, can not migrate int with limit 19Heroku pg,无法迁移限制为 19 的 int
【发布时间】:2018-01-04 22:38:19
【问题描述】:

我有一个数据库列作为整数设置为限制 19。它在 localhost 上工作正常,因为我有 sqlite。但我无法在 Heroku pg 数据库上迁移。

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

然后我尝试将列从整数更改为字符串,这样会更好。但是因为它无法进行heroku run rake db:migrate 我无法更改列类型。我该怎么办?

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

【问题讨论】:

  • 您不能删除 AdddivIdToLocations 迁移并添加一个新的迁移,以将列创建为字符串吗?在此过程中,您可能不得不弄乱您的开发 SQLite 数据库,但这只是提醒您,您不应该在 SQLite 上进行开发并在 PostgreSQL 上进行部署。我强烈建议在您的开发环境中安装 PostgreSQL,以便您的所有三个环境(开发、测试、生产)都使用相同的堆栈。

标签: ruby-on-rails sqlite heroku pg


【解决方案1】:

div_id 的最大值是多少?

:limit 指定最大存储大小(以字节为单位) - 可用值如下:

+------------------------------------------------------------+
| :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 |
+------------------------------------------------------------+

如果您未指定任何值,则默认为 4 INT(11)。如果您的值大于整数,则设置 limit: 8 以便它可以存储 BIGINT。 19 不是有效值。

您可以更新 limit: 部分迁移并再次运行 db:migrate。 如果要更改列类型,则首先必须存在此列,为此运行 db:migrate without limit:

【讨论】:

    猜你喜欢
    • 2014-06-17
    • 2015-10-25
    • 2021-12-14
    • 2017-07-21
    • 2013-01-10
    • 2016-02-02
    • 2021-06-17
    • 2020-07-09
    • 2021-09-25
    相关资源
    最近更新 更多