2013-07-29 48 views
17

我有一个Listings controller和用户可以添加说明。如果描述很长,应该是这样的,我在Heroku中收到这个错误:PG :: String DataRightTruncation:ERROR:PostgreSQL string(255)limit | Heroku

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

我该如何解决这个问题?

编辑

我发现(约翰说也),我有我的表中的字符串(其中有一个限制)更改为:文字是无限的。但只是在迁移中更改表格似乎没有用。

我已编辑的房源迁移

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 

回答

21

这似乎是指定列一个:文本类型,而不是一个:字符串会解决这个问题。

+0

是的只是基金那,但我有问题得到这个工作。如果我只是更新迁移,它不会在heroku上工作。我会编辑我的问题 –

+0

你的移植应该是't.text:description,:limit => nil'不't.string' –

+0

嗯:(我仍然遇到heroku麻烦 –

2

给定的输入是一个string场太长,所以才更改为text场:

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

然后运行rake db:migrate

10

不要编辑您以前的迁移。作为一个规则,永远不要那样做。相反,你将创建一个新的迁移,这将使变化(并让你回滚如有必要)。

首先,生成迁移:所以,看起来像

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异常。)

相关问题