B Seven
B Seven

Reputation: 45941

Why is there an error for a character limit of 255 on text field in PostgreSQL?

After finding 2 SO questions related to string vs. text fields, I discovered that the link field is already text:

create_table "answers", :force => true do |t|
  t.string   "text"
  t.text     "link"

Why is Postgres throwing the following error:

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

I tried a migration change_column :answers, :link, :text, :limit => nil but I don't think that does anything.

Edit: So, the problem is that the schema says the field is text, but actually it is string:

Answer.new.column_for_attribute('link').type
=> :string

What is the best way to fix it? By rebuilding the database from the schema? By changing it to string and then back to text?

Rails 3.2.2, Postgres 9, Heroku.

Upvotes: 3

Views: 2526

Answers (2)

B Seven
B Seven

Reputation: 45941

Not quite sure how it happened, but one of the following:

  1. I checked the schema after running the migration, so actually it was in sync.
  2. I ran a migration to change the type to string, bringing the schema in sync.

Upvotes: 1

d11wtq
d11wtq

Reputation: 35318

Write your migration using raw SQL statements sent to Postgres. Just call execute with whatever you'd run directly in the psql console. It's the best way to know exactly what state your DB is going to be left in. Often this is a better solution than letting Rails define your schema for you. You should really look at your schema regularly, especially after running a migration.

Upvotes: 0

Related Questions