Setting :binary, :limit option on mysql column

How do I add a binary type column with limit option to a MySQL database
with migrations?

Here’s what I have for a migration right now:

  1. def self.up
  2. add_column :table_name, :column_name, :binary, :limit =>
    

1.megabyte
3. end

Then I ‘rake db:migrate’:

  1. – add_column(:table_name, :column_name, :binary,
    {:limit=>1048576})
  2. -> 0.0165s

When I check the schema:

  1. t.column “column_name”, :binary

Without a limit option! And even though it appears to migrate, the limit
in the application definitely does not change.

Can anyone tell me what to do differently? Thanks.

On Jul 24, 1:09 pm, Patrick Berkeley <rails-mailing-l…@andreas-
s.net> wrote:

How do I add a binary type column with limit option to a MySQL database
with migrations?

Without a limit option! And even though it appears to migrate, the limit
in the application definitely does not change.

I use the limit option, and although it does not appear in my schema
file, it does change the size of the binary column. I haven’t tested
to see if it creates an upper bound, but it does change the BLOB type.

The four BLOB types are TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB.

For example, a :limit=>256.kilobyte creates a MEDIUMBLOB instead of a
BLOB (default).

If you want an upper bound on length, maybe create a validation in
your model?
http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M000942

~Rusty

Rusty B. wrote:

On Jul 24, 1:09 pm, Patrick Berkeley <rails-mailing-l…@andreas-
s.net> wrote:

How do I add a binary type column with limit option to a MySQL database
with migrations?

Without a limit option! And even though it appears to migrate, the limit
in the application definitely does not change.

I use the limit option, and although it does not appear in my schema
file, it does change the size of the binary column. I haven’t tested
to see if it creates an upper bound, but it does change the BLOB type.

The four BLOB types are TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB.

For example, a :limit=>256.kilobyte creates a MEDIUMBLOB instead of a
BLOB (default).

If you want an upper bound on length, maybe create a validation in
your model?
ActiveRecord::Validations::ClassMethods

~Rusty

That’s good info. I had been writing a custom validation to check the
length. I’m assuming each step up in BLOB type doubles the size. So,
TINYBLOB = 64k, BLOB = 128k, MEDIUMBLOB = 256k, LONGBLOB = 512k. Thanks.