Rails - How to add a BLOB column using ActiveRecord?

Amokrane Chentir picture Amokrane Chentir · Sep 5, 2011 · Viewed 14.1k times · Source

I need to create a BLOB column to store some text content.

I have read somewhere that I need to do the following:

class AddVersionCommentToMetaData < ActiveRecord::Migration
  def self.up
    add_column :meta_data, :version_comment, :binary, :limit => 10.megabyte
  end

  def self.down
    remove_column :meta_data, :version_comment
  end
end

However, it gives the following error message:

PGError: ERROR: type modifier is not allowed for type "bytea" LINE 1: ..."meta_data" ADD COLUMN "version_comment_extended" bytea(1048... ^ : ALTER TABLE "meta_data" ADD COLUMN "version_comment_extended" bytea(10485760)

Any idea?

Please note that I am using PostgreSQL. Thanks!

Answer

amit_saxena picture amit_saxena · Sep 5, 2011

The migration seems to be correct except the down part. It should be:

class AddVersionCommentToMetaData < ActiveRecord::Migration
  def self.up
    add_column :meta_data, :version_comment, :binary, :limit => 10.megabyte
  end

  def self.down
    remove_column :meta_data, :version_comment
  end
end

double check for typos. And what version of rails are you using? It works well in rails 3.0.7.