2013-07-29 47 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異常。)

相關問題