Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - How to add a BLOB column using ActiveRecord?

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!

like image 968
Amokrane Chentir Avatar asked Feb 02 '23 14:02

Amokrane Chentir


1 Answers

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.

like image 147
amit_saxena Avatar answered Feb 13 '23 16:02

amit_saxena