Search code examples
mysqlruby-on-railsbigintschema.rb

How to make Rails generate 'schema.rb' with bigint support for MySQL?


I am using Rails 3.0.5. I am using MySQL as a database storage. I have a model in which one of the columns needs to be BIGINT. I am using the following in my create migration file:

t.column  :my_column_name,       :bigint

which works fine.

However, when I run

rake db:migrate

the generated 'schema.rb' file creates the following line for the particular column:

t.integer  "my_column_name",       :limit => 8

which is not correct.

My question is where am I wrong with that? Is there something that I should do in order to get the correct 'schema.rb' file? Can I change the way 'schema.rb' file is generated?

Please, note that the fact that the 'schema.rb' file is wrong causes problems to my continuous integration server, which runs tests and creates the db from scratch (before running tests) using the 'schema.rb' file.


Solution

  • I now realize that

    t.integer "my_column_name", :limit => 8
    

    with my_sql driver is CORRECT in the schema.rb file.

    The database that is generated using the 'schema.rb' file creates a

    bigint(20)
    

    though this may seem strange.

    I have found this by investigating the code of my_sql adapter, the snippet of which I quote here:

     def type_to_sql(type, limit = nil, precision = nil, scale = nil)
           return super unless type.to_s == 'integer'
            case limit
            when 1; 'tinyint'
            when 2; 'smallint'
            when 3; 'mediumint'
            when nil, 4, 11; 'int(11)'  # compatibility with MySQL default
            when 5..8; 'bigint'
            else raise(ActiveRecordError, "No integer type has byte size #{limit}")
            end
          end
    

    It is clear, there that :limit => 8 will end up creating a bigint in mysql db.