PostgreSQL string(255) limit - Rails, Ruby and Her

2020-05-24 20:39发布

问题:

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.

回答1:

You would need to use text instead of string.

Migration would be something along the lines of:

change_column :comments, :body, :text, :limit => nil


回答2:

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