10

I'm trying to write a migration that adds a LONGBLOB column to a table in a MySQL database. I'd like to use LONGBLOB instead of BLOB so that I can store more data in the binary column. The problem is that it adds a BLOB column even though I specify a larger size.

Here's the line I'm using to add the column:

add_column :db_files, :data, :binary, :null => false, :size => 1.megabyte

Am I doing this incorrectly?

readonly
  • 343,444
  • 107
  • 203
  • 205

2 Answers2

32

The following will create a MEDIUMBLOB field. Use 16.megabyte to go to a LONGBLOB.

def self.up
  create_table "blob_test", :force => true do |t|
    t.column :data, :binary, :limit => 10.megabyte
  end
end
AnkitG
  • 6,438
  • 7
  • 44
  • 72
Michael Glenn
  • 1,872
  • 1
  • 19
  • 23
  • 1
    FYI, altering a `:binary` column in attempting to increase its size (changing a regular BLOB to a LONGBLOB) does not seem to work, if done via the Rails migration API (as of Rails 2.3.5). – Chris W. Apr 07 '11 at 15:41
  • Actually, that may not be true... I think I may have been using the wrong parameter -- `:size` instead of `:limit`... But I went ahead and used raw SQL via `execute`. Note that, the original poster used the latter name (for specifying `1.megabyte`), while `:limit` seems to be the correct parameter (as given in the answer above). – Chris W. Apr 07 '11 at 16:04
  • Altering an existing blob to increase its size does seem to work with rails 4.1. But make sure you specify the column type as :binary and not :blob. If you specify it as :blob, even with a large :limit, you just get a blob. With :binary and a large :limit you get a longblob. E.g. "change_column :mytable, :mycolumn, :binary, :limit => 16.megabyte" – Jason Heiss Jun 09 '14 at 22:18
-4

class Migration_create_technologe extends CI_Migration {

public function up()
{
    $this->dbforge->add_field(array(
        'id' => array(
            'type' => 'INT',
            'constraint' => 11,
            'unsigned' => TRUE,
            'auto_increment' => TRUE
        ),
        'title' => array(
            'type' => 'VARCHAR',
            'constraint' => '100',
            'collation' => 'utf8_unicode_ci',
        ),
        'writer' => array(
            'type' => 'VARCHAR',
            'constraint' => '100',
            'collation' => 'utf8_unicode_ci',
        ),
        'created' => array(
            'type' => 'datetime',
             'default'=> '0000-00-00 00:00:00',
            'collation' => 'utf8_unicode_ci',
            ),                           
    ));
    $this->dbforge->add_key('id',TRUE);
   $this->dbforge->add_field("body longblob NOT NULL ");
    $this->dbforge->create_table('technologe');
}

public function down()
{
    $this->dbforge->drop_table('technologe');
}
}