Rails + 回形针范围错误

Rails + Paperclip RangeError

我正在开发一个允许使用 Paperclip 上传视频的 Rails 4.2.5 应用程序。除了非常大的文件外,它工作正常。使用超过 3GB 的视频时出现以下错误:

RangeError (3283091012 is out of range for ActiveRecord::Type::Integer with limit 4)

现在我可以知道 3283091012 是文件的大小,所以我认为当 MySQL 试图为回形针创建的 file_size 字段保存该值时会发生这种情况,并且我可以通过增加数据库的限制或使用迁移文件更好地修复它,但我想我之前会检查 table 结构,我对以下内容感到惊讶:

mysql> describe formats;
+-------------------+--------------+------+-----+---------+----------------+
| Field             | Type         | Null | Key | Default | Extra          |
+-------------------+--------------+------+-----+---------+----------------+
| id                | int(11)      | NO   | PRI | NULL    | auto_increment |
| video_id          | int(11)      | YES  | MUL | NULL    |                |
| name              | varchar(255) | YES  |     | NULL    |                |
| quality           | varchar(255) | YES  |     | NULL    |                |
| created_at        | datetime     | NO   |     | NULL    |                |
| updated_at        | datetime     | NO   |     | NULL    |                |
| file_file_name    | varchar(255) | YES  |     | NULL    |                |
| file_content_type | varchar(255) | YES  |     | NULL    |                |
| file_file_size    | int(11)      | YES  |     | NULL    |                |
| file_updated_at   | datetime     | YES  |     | NULL    |                |
| trailer           | tinyint(1)   | YES  |     | 0       |                |
+-------------------+--------------+------+-----+---------+----------------+

如您所见,file_file_size 是一个 int(11) 而不是错误所声称的 4。 知道是什么原因造成的吗?

注意:我没有对模型进行任何花哨的验证(只是强制存在几个属性)并且控制器操作是常规创建操作。

根据 MYSQL 文档,整数的范围从 -2147483648 到 +2147483647。所以你的号码对于这种类型来说太大了。

更新您的专栏并使用参数 limit 来表明您想要一个 bigint。

class ChangeIntegerLimitInYourTable < ActiveRecord::Migration
  def change
    update_column :your_table, :your_column, :integer, limit: 8
  end 
end

当 运行 迁移时,Srv 的回答给我一个 undefined method 'update_column' 错误,但这有效:

class ChangeIntegerLimitInFormats < ActiveRecord::Migration
  def self.up
    change_table :formats do |t|
      t.change :file_file_size, :integer, limit: 8
    end
  end
  def self.down
    change_table :formats do |t|
      t.change :file_file_size, :integer, limit: 4
    end
  end
end

在 运行 迁移后检查 table 结构显示列 file_file_size 现在是 Int(20) 而不是像以前那样的 Int(11),不确定 4 是如何转换为 11 而 8 是如何转换为 20,这是一开始的混乱根源,如果有人知道这方面的信息,请添加评论。

int(11) 中的 11 和 int(20) 中的 20 是这些列的显示宽度。它们不反映用于存储整数的位数;相反,它们可以用于零填充。

11 是 INT 的默认值,因为带符号的 4 字节整数的最大宽度为 -2147483648。同样,20 是 BIGINT 的默认值,因为 8 字节整数的最大宽度为 -9223372036854775808。

事实上,我希望 file_file_size 在更新列后成为一个 bigint(20)。

这里有一个很好的解释。 https://blogs.oracle.com/jsmyth/what-does-the-11-mean-in-int11