Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

So I have a comments table that is structured like this:

# == Schema Information
#
# Table name: comments
#
#  id         :integer         not null, primary key
#  body       :string(255)
#  notified   :boolean
#  user_id    :integer
#  stage_id   :integer
#  created_at :datetime
#  updated_at :datetime
#  client_id  :integer
#  author     :string(255)

This is the error message I am getting:

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

How do I store long text in a PG column using Rails 3.x and Heroku?

What would the migration look like to fix this issue?

Thanks.

share|improve this question
1  
Are you looking for anything more than: stackoverflow.com/questions/4103141/… ? – John Plummer Mar 20 '12 at 10:20
    
No...that's exactly what I was looking for. Thanks! – marcamillion Mar 20 '12 at 11:17

2 Answers 2

up vote 56 down vote accepted

You would need to use text instead of string.

Migration would be something along the lines of:

change_column :comments, :body, :text, :limit => nil
share|improve this answer
6  
One change to that I would make is, make sure to specify :limit => nil, otherwise a limit of 255 will be added to the schema in Rails 3.x. – marcamillion Mar 20 '12 at 11:19
4  
The limit:nil is no longer necessary. Using rails 3.2.11, setting type to :text sets the Postgres column type to 'text'. There is no implicit limit:255. – Noach Magedman Feb 5 '13 at 9:30
    
Rails 3.2.8 , :limit => nil is needed. – K M Rakibul Islam Mar 26 '13 at 16:43
    
I ran this migration the other day using 4.0.3 and needed the limit => nil. – Marina Martin Apr 22 '14 at 0:16
    
If it's any consolation, I just did this in rails 4.0.3 (hosting on heroku) and did not need to set this to nil in order for it to work. It is strange that some seem to need to do so and others dont. – Sean Callahan Apr 22 '14 at 0:25

All the time I solve this problem by this type of query
ALTER TABLE your_table_name ALTER COLUMN your_column_name TYPE text;


Character varying has limited length and you can not pass this length.
text is a variable which has no limit.
So you can convert your column type from character varying(which has a length) to
text (which has no limit).

share|improve this answer

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.