PG :: StringDataRightTruncation:错误:PostgreSQL字符串(255)限制| Heroku的

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

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

我怎样才能解决这个问题?

编辑

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

我编辑的列表迁移

 class CreateListings  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 

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

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

首先,生成迁移:

 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 :: IrreversibleMigrationexception。)

对于string字段,给定的输入太长,因此只需更改为text字段:

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

然后运行rake db:migrate