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

Min*_*ohn 17 ruby-on-rails heroku ruby-on-rails-4

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

ActiveRecord::StatementInvalid (PG::StringDataRightTruncation: ERROR:  
value too long for type character varying(255)
Run Code Online (Sandbox Code Playgroud)

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

编辑

我发现(John也说)我必须将我的表字符串(有一个限制)更改为: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
Run Code Online (Sandbox Code Playgroud)

但我仍然遇到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
Run Code Online (Sandbox Code Playgroud)

Joh*_*non 21

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

  • 确保在运行迁移后重新启动(`heroku restart`). (2认同)

Ton*_*ito 10

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

首先,生成迁移:

rails g migration change_datatype_on_TABLE_from_string_to_text
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

现在运行迁移:

bundle exec rake db:migrate
Run Code Online (Sandbox Code Playgroud)

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