【问题标题】:RoR Postgresql on Heroku error: PG::StringDataRightTruncation [duplicate]Heroku 错误上的 RoR Postgresql:PG::StringDataRightTruncation [重复]
【发布时间】:2015-05-17 11:14:49
【问题描述】:

我有一个 ruby​​ on rails 项目,允许我提交评论(称为注释)。它似乎在我的计算机上运行良好,我正在使用 sql 或任何带有 rails 的库存。

在我将应用程序放在 heroku 上后,一切都运行良好,除了现在我的笔记超过 255 个字符返回错误。

我很确定这与数据库有关,因为在 localhost:3000 上我可以保存数百行文本。

但是,我对数据库一无所知 - 有人可以通过此错误为我指明正确的方向吗?错误是:

PG::StringDataRightTruncation: 错误: 值对于类型来说太长了 字符变化(255)

这是笔记的迁移,它被定义为一个字符串,而不是一个字符:

class CreateNotes < ActiveRecord::Migration
  def change
    create_table :notes do |t|
      t.string :title
      t.string :comment
      t.integer :user_id
      t.integer :record_id

      t.timestamps
    end
  end
end

【问题讨论】:

标签: ruby-on-rails postgresql heroku


【解决方案1】:

您应该为包含超过 255 个字符的属性使用 :text 字段类型。

class CreateNotes < ActiveRecord::Migration
  def change
    create_table :notes do |t|
    t.text :title
    t.text :comment
    t.integer :user_id
    t.integer :record_id

    t.timestamps
  end
end

这只有在您能够通过以下方式重新创建数据库时才有效

rake db:drop 
rake db:create
rake db:migrate

如果您无法重置数据库,则必须使用

进行新的迁移
def change
  change_column :notes, :title, :text   
  change_column :notes, :comment, :text   
end

【讨论】:

  • 我已经进行了更改,但我不知道如何在不破坏数据库中所有数据的情况下将更改推送到 heroku。 “heroku run rake db:migrate --app [appname]”确实有帮助
  • 更新了我的答案以反映在不破坏数据的情况下更新生产数据库。确保您正在创建一个新的迁移
猜你喜欢
  • 1970-01-01
  • 2013-07-29
  • 1970-01-01
  • 1970-01-01
  • 2014-03-28
  • 2015-03-20
  • 1970-01-01
  • 2015-08-10
  • 2018-08-18
相关资源
最近更新 更多