Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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
share|improve this question
add comment

4 Answers

up vote 9 down vote accepted

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

share|improve this answer
    
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 –  The Mini John Jul 29 '13 at 9:24
    
your migration should be t.text :description, :limit => nil not t.string –  John Beynon Jul 29 '13 at 9:34
    
Well :(, i'm still getting heroku trouble. –  The Mini John Jul 29 '13 at 9:40
1  
make sure you restart (heroku restart) after you've run the migrations. –  John Beynon Jul 29 '13 at 10:19
1  
This should be the accepted answer. See my comment on the other answer. –  Lukas_Skywalker Dec 16 '13 at 8:34
show 1 more comment

So after a while, i found a Solution that worked. I'm new to rails so if this is not the best practice bear with me.

Actually just changing t.string into t.text for the :description in the migration wont do the trick, because in the database schema :description is still t.string.

The difference relies in how the symbol is converted into its respective column type in query language.

:string |                   VARCHAR             | :limit => 1 to 255 (default = 255)  
:text   | TINYTEXT, TEXT, MEDIUMTEXT, LONGTEXT2 | :limit => 1 to 4294967296 (default = 65536)2

When should each be used?

As a general rule of thumb, use :string for short text input (username, email, password, titles, etc.) and use :text for longer expected input such as descriptions, comment content, etc.

However the PostsgreSQL Manual states:

If you are using postgres use text wherever you can, unless you have a size constraint since there is no performance penalty for text vs varchar.

There is no performance difference among these three types, apart from increased storage space when using the blank-padded type, and a few extra CPU cycles to check the length when storing into a length-constrained column. While character(n) has performance advantages in some other database systems, there is no such advantage in PostgreSQL; in fact character(n) is usually the slowest of the three because of its additional storage costs. In most situations text or character varying should be used instead

So how to Actually change that in your Database

1.) Just generate a migration that changes the string into text

2.) The Mini John's way (cause i'm a noob)

Because i generated the description input along with my Scaffold i could not overwrite it (Bear with me) in a way that it would also change it in the Database Schema.

So i accessed my Database via the SQlite Database Browser and simply deleted the description table. I took out (from my initial scaffold generation) the description input and run a new migration

rails g migration AddDescriptionToListings description:text

Rake db:migrate and it was done.

ON HEROKU - Warning

Only do this if you  have data in your Database that you can mind to loose !!!

Because on Herokus PGSQL the migrations and all the stuff still exist's i wiped clean the database:

heroku pg:reset DATABASE

after that you simply run your migrations again

heroku run rake db:migrate

And all works :)

Hope that others will find that helpful and save them some time

share|improve this answer
5  
This is certainly not how you should do it. Migrations are created so you can get back to any previous schema if you need to. You should never change old migrations. Just create a new migration that runs change_column :listings, :description, :text and run rake db:migrate as well as heroku run rake db:migrate in order for the schema to be updated. No data loss. If you want to change your inputs, change f.text_field to f.text_area. But please, please, don't give people the advice to drop their database. Even with a warning. –  Lukas_Skywalker Dec 16 '13 at 8:32
1  
Your comment is the correct way to do it, and I find it crazy that people post comment like The Mini John one, "yeah, just drop your database on Heroku" –  Dorian Mar 11 at 15:25
add comment

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

share|improve this answer
add comment

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.)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.