Reputation: 2759
When I run $heroku db:push, I get the following error:
Saving session to push_201110231302.dat..
!!! Caught Server Exception
HTTP CODE: 500
Taps Server Error: PGError: ERROR: value too long for type character varying(255)
The discussion here suggests that this is because one of my models has a string attribute that's longer than 255 chars.
I think I identified the problem in the "descrip" attribute of my "Course" model.
I tried fixing this by changing descrip's type from string to text. I did this by generating and running a "remove_descrip_from_Course descrip:string" type migration, then generating/running an "add_descrip_to_Course descrip:text." But that didn't work -- descrip still shows up as a string.
Does anyone know if (1) changing descrip from a string to text is the best (only?) way of solving the heroku problem, and (2) what I can do to change descrip from string to text?
EDITED:
Here's the relevant portion of my schema.rb file (descrip is at bottom):
create_table "courses", :force => true do |t|
t.string "name"
t.string "number"
t.string "instructor"
t.string "room"
t.string "day"
t.string "units"
t.string "time"
t.datetime "created_at"
t.datetime "updated_at"
t.string "limitations"
t.string "address"
t.string "end_time"
t.string "start_time"
t.string "crn"
t.string "term_code"
t.text "evals"
t.boolean "paper_required"
t.string "exam_type"
t.string "paper_type"
t.string "past_instructors"
t.string "past_semesters"
t.string "tod"
t.boolean "in_cart", :default => false
t.integer "day_num"
t.integer "time_num"
t.string "units_alt"
t.text "descrip"
end
And here's the add_descrip_to_course migration:
class AddDescripToCourse < ActiveRecord::Migration
def self.up
add_column :courses, :descrip, :text
end
def self.down
remove_column :courses, :descrip
end
end
But this is what happens in my rails console:
ruby-1.9.2-p290 :008 > a = Course.new
=> #<Course id: nil, name: nil, number: nil, instructor: nil, room: nil, day: nil, units: nil, time: nil, created_at: nil, updated_at: nil, limitations: nil, address: nil, end_time: nil, start_time: nil, crn: nil, term_code: nil, evals: nil, paper_required: nil, exam_type: nil, paper_type: nil, past_instructors: nil, past_semesters: nil, tod: nil, in_cart: false, day_num: nil, time_num: nil, units_alt: nil, descrip: nil>
ruby-1.9.2-p290 :009 > a.descrip = "test"
=> "test"
ruby-1.9.2-p290 :010 > a.descrip.class
=> String
Upvotes: 1
Views: 1397
Reputation: 4539
I had a similar problem at one time (I had to change a string to a text), and ran into the same obstacles. I eventually had to make a migration that dropped the entire table and recreated again with the proper column types.
Upvotes: 0
Reputation: 434585
Your first step is to develop and deploy on the same stack; developing on top of MySQL or SQLite and then deploying on top of PostgreSQL is just asking for trouble, PostgreSQL is (thankfully) a lot stricter than SQLite and MySQL. So install PostgreSQL locally if you're going to be deploying to Heroku.
To change the column type, you should be able to use this in a migration:
def self.up # or "def change" or "def up" depending on your Rails version
change_column :courses, :descrip, :text
end
That should give you a column of type TEXT
in PostgreSQL and TEXT
is a "variable unlimited length" character type.
Using :text
for this is the best way to get an arbitrarily large text column.
If you don't really want an unlimited length then you should include length validations in your model. MySQL will silently truncate your data and SQLite ignores length on string columns, PostgreSQL does neither of these things.
In the Rails/ActiveRecord world, all stringish types are instances of String so char(n)
, varchar(n)
, and text
all come out as String and go in as String.
Upvotes: 2