1

I have the following Rails Migration:

 class ChangeCommentLength < ActiveRecord::Migration
   def up
     change_column :videos, :comment, :string, limit: 500
   end

   def down
     change_column :videos, :comment, :string, limit: 255
   end
 end

However, when I run this, I get the following error message:

PG::Error: ERROR:  cannot alter type of a column used by a view or rule
DETAIL:  rule _RETURN on -view- depends on column "comment"

Attempting to run the sql directly:

ALTER TABLE "videos" ALTER COLUMN "comment" TYPE character varying(500);
ERROR:  cannot alter type of a column used by a view or rule
DETAIL:  rule _RETURN on view -view- depends on column "comment"

I understand why this would usually be a problem (can't go modifying datatypes underneath existing views). However, how can I work around this given that I am not changing the column type, just the column limit?

Abraham P
  • 15,029
  • 13
  • 58
  • 126
  • How about writing the migrations in plain SQL. Or would that just result in the same SQL code generated by Rails and thus the same PG error? – zwippie Apr 14 '14 at 18:49
  • I've made an attempt to run the sql directly through rails:dbconsole with the same result, and have ammended my post to reflect that – Abraham P Apr 14 '14 at 18:59
  • 2
    Can you drop and recreate the view? And while you're at it, go straight to `:text` so that you don't have to worry about the size (and toss in a CHECK if you do have a real length constraint to worry about). – mu is too short Apr 14 '14 at 20:00
  • 1
    Details for @mu's suggestion: http://stackoverflow.com/questions/8524873/change-postgresql-columns-used-in-views/8527792#8527792 – Erwin Brandstetter Apr 14 '14 at 20:07

0 Answers0