17

I Have a Listings controller and users can add a description. If the description is long, which it should be, I receive this error in Heroku:

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

How can I fix this?

Edit

I found out (John said it also) that i have to change in my table string(which has a limit) to :text which is limitless. But only changing the table in the migration didn't seem to work.

My Edited Listings Migration

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

  t.timestamps
end
end
end

But i'm still getting Heroku trouble ->

    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

3 Answers 3

21

It would seem that specifying the column as a :text type and not a :string would fix this problem.

Sign up to request clarification or add additional context in comments.

6 Comments

Yeah just fund that out but i'm having problems getting this to work. If i just update the migration it wont work on heroku. I'll edit my Question
your migration should be t.text :description, :limit => nil not t.string
Well :(, i'm still getting heroku trouble.
make sure you restart (heroku restart) after you've run the migrations.
This should be the accepted answer. See my comment on the other answer.
|
11

Do not edit your previous migrations. Never do that, as a rule. Instead you'll create a new migration that will make the change (and allow you to rollback if necessary.)

First, generate the migration:

rails g migration change_datatype_on_TABLE_from_string_to_text

Then edit the generated file so that looks something like (change the table and column names as necessary):

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

Now run the migration:

bundle exec rake db:migrate

That should do it. (Note I defined the up and down methods individually instead of using the change method because using the change method only works with reversible migrations, and trying to rollback a datatype change would throw a ActiveRecord::IrreversibleMigration exception.)

Comments

2

The input given is too long for a string field, so just change to a text field:

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

Then run rake db:migrate

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.