Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

TableDefinition.column ignores :limit for integer types

#1819

Given a migration like:


class CreateMyModel < ActiveRecord::Migration
  def self.up
    create_table :my_models, :id => false do |t|
      t.column  :integer_test1, :integer, :limit => 20
      t.integer :integer_test2, :limit => 20
      t.string  :string_test1, :limit => 20
    end
  end
end

It generates a schema like:


+---------------+--------------+------+-----+---------+----------------+
| Field         | Type         | Null | Key | Default | Extra          |
+---------------+--------------+------+-----+---------+----------------+
| id            | int(11)      | NO   | PRI | NULL    | auto_increment | 
| integer_test1 | int(11)      | YES  |     | NULL    |                | 
| integer_test2 | int(11)      | YES  |     | NULL    |                | 
| string_test1  | varchar(20)  | YES  |     | NULL    |                | 
+---------------+--------------+------+-----+---------+----------------+

when the integer columns should be of type int(20). This happens with MySQL and SQLite, so I suspect it is a bug on the column method.

Reported by Carlos Paramio · January 29th, 2009 @ 04:19 PM

State: invalid
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Carlos Paramio
    Carlos Paramio

    I realized that this fails because there isn't any integer type on MySQL with a size of 20 bytes:

    ActiveRecord::Base.connection.type_to_sql(:integer, 20) ActiveRecord::ActiveRecordError: No integer type has byte size 20

    Please close this ticket.

    January 29th, 2009 @ 04:43 PM

  2. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from 2.2.2, activerecord, limit, schema to 2.2.2, activerecord, bugmash, limit, schema

    August 4th, 2009 @ 06:16 PM

  3. Rajesh
    Rajesh

    +1 for closing this ticket

    ActiveRecord::Base.connection.type_to_sql(:integer, 20)
    ActiveRecord::ActiveRecordError: No integer type has byte size 20

    from /Users/rajesh/src/property/trunk/vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb:535:in `type_to_sql'
    

    but following work fine

    ActiveRecord::Base.connection.type_to_sql(:integer, 4) => "int(11)" ActiveRecord::Base.connection.type_to_sql(:integer, 8) => "bigint"

    August 8th, 2009 @ 09:12 PM

  4. Hugo Peixoto
    Hugo Peixoto

    In mysql, I'm able to create a table with int(20):

    mysql> create table test(id int(20));
    mysql> show columns from test;
    +-------+---------+------+-----+---------+-------+
    | Field | Type    | Null | Key | Default | Extra |
    +-------+---------+------+-----+---------+-------+
    | id    | int(20) | YES  |     | NULL    |       |
    +-------+---------+------+-----+---------+-------+
    

    If I insert large values (like 9999999999999999999) they won't be saved.

    
    mysql> insert into test VALUES (9999999999999999999);
    mysql> select id from test;
    +------------+
    | id         |
    +------------+
    | 2147483647 |
    +------------+
    1 row in set (0.00 sec)
    

    int(20) only does something if you use it in conjunction with ZEROFILL,
    in which the mysql interactive tools will print out something like this:

    mysql> drop table test;
    mysql> create table test(id int(20) zerofill);
    mysql> insert into test VALUES (9999999999999999999);
    mysql> select id from test;
    +----------------------+
    | id                   |
    +----------------------+
    | 00000000004294967295 |
    +----------------------+
    

    Odd behaviour, but since it doesn't change the storage size, I'd say it's irrelevant for rails purpose. I say +1 for closing this ticket.

    August 8th, 2009 @ 10:54 PM

  5. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from 2.2.2, activerecord, bugmash, limit, schema to 2.2.2, activerecord, limit, schema
    • State changed from new to invalid

    Not worth changing Rails core unless we see some use case and patch that isn't mysql-specific.

    August 9th, 2009 @ 03:21 PM