【问题标题】:PG::StringDataRightTruncation: ERROR: PostgreSQL string(255) limit | HerokuPG::StringDataRightTruncation: 错误: PostgreSQL 字符串 (255) 限制 | Heroku
【发布时间】:2013-07-29 00:09:11
【问题描述】:

我有一个Listings controller,用户可以添加描述。如果描述很长(应该是这样),我会在 Heroku 中收到此错误:

ActiveRecord::StatementInvalid (PG::StringDataRightTruncation: ERROR:  
value too long for type character varying(255)

我该如何解决这个问题?

编辑

我发现(约翰也说过)我必须将我的表字符串(有限制)更改为 :text 是无限的。但是只在迁移中更改表似乎不起作用。

我的已编辑列表迁移

class CreateListings < ActiveRecord::Migration
def change
create_table :listings do |t|
  t.string :title
  t.text :description, :limit => nil

  t.timestamps
end
end
end

但我仍然遇到 Heroku 的麻烦 ->

    2013-07-29T09:39:05.069692+00:00 app[web.1]: ActiveRecord::StatementInvalid (PG::StringDataRightTruncation: ERROR:  value too long for type character v rying(255)
2013-07-29T09:39:05.069870+00:00 app[web.1]:
2013-07-29T09:39:05.069692+00:00 app[web.1]: : INSERT INTO "listings" ("created_at", "description", "image_content_type", "image_file_name", "image_fil _size", "image_updated_at", "price", "title", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id"):
2013-07-29T09:39:05.069870+00:00 app[web.1]:   app/controllers/listings_controller.rb:35:in `block in create'
2013-07-29T09:39:05.069870+00:00 app[web.1]:   app/controllers/listings_controller.rb:34:in `create'
2013-07-29T09:39:05.069870+00:00 app[web.1]:
2013-07-29T09:39:05.069860+00:00 heroku[router]: at=info method=POST path=/listings host=vaultx.herokuapp.com fwd="178.59.173.169" dyno=web.1 connect=3 s service=1882ms status=500 bytes=1266

【问题讨论】:

    标签: ruby-on-rails heroku ruby-on-rails-4


    【解决方案1】:

    将列指定为 :text 类型而不是 :string 似乎可以解决此问题。

    【讨论】:

    • 是的,只是为它提供资金,但我在让它发挥作用时遇到了问题。如果我只是更新迁移,它将无法在 heroku 上运行。我将编辑我的问题
    • 您的迁移应该是t.text :description, :limit =&gt; nil 而不是t.string
    • 嗯 :(,我还是遇到了 heroku 的麻烦。
    • 确保在运行迁移后重新启动 (heroku restart)。
    • 这应该是公认的答案。请参阅另一个答案上的my comment
    【解决方案2】:

    给定的输入对于string 字段来说太长,所以只需更改为text 字段:

    class ChangeListingsDescriptionTypeToText < ActiveRecord::Migration
      def change
        change_column :listings, :description, :text
      end
    end
    

    然后运行rake db:migrate

    【讨论】:

      【解决方案3】:

      请勿编辑您之前的迁移。作为一项规则,永远不要这样做。相反,您将创建一个新的迁移来进行更改(并允许您在必要时回滚。)

      首先,生成迁移:

      rails g migration change_datatype_on_TABLE_from_string_to_text
      

      然后编辑生成的文件,使其看起来像(根据需要更改表和列名称):

      class ChangeDatatypeOnTableFromStringToText < ActiveRecord::Migration
        def up
          change_column :table_name, :column_name, :text, :limit => nil
        end
      
        def down
          change_column :table_name, :column_name, :string
        end
      end
      

      现在运行迁移:

      bundle exec rake db:migrate
      

      应该这样做。 (注意我单独定义了 up 和 down 方法,而不是使用 change 方法,因为使用 change 方法仅适用于可逆迁移,并且尝试回滚数据类型更改会引发 ActiveRecord::IrreversibleMigration 异常。)

      【讨论】:

        猜你喜欢
        • 2012-04-04
        • 2014-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-28
        相关资源
        最近更新 更多