Forum: Ruby on Rails setting :binary, :limit option on mysql column

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Patrick B. (Guest)
on 2007-07-24 21:09
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.
Rusty B. (Guest)
on 2007-07-26 02:45
(Received via mailing list)
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?
<snip />
> 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/Va...

~Rusty
Patrick B. (Guest)
on 2007-07-26 03:49
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?
> <snip />
>> 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/Va...
>
> ~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.
This topic is locked and can not be replied to.