Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

Added that the MySQL adapter should map integer to either smallint, i…

…nt, or bigint depending on the :limit just like PostgreSQL [DHH]
  • Loading branch information...
commit a37546517dad9f6d9a7de6e1dba4d960909d71e8 1 parent 1959db3
David Heinemeier Hansson authored
2  activerecord/CHANGELOG
@@ -1,5 +1,7 @@
+* Added that the MySQL adapter should map integer to either smallint, int, or bigint depending on the :limit just like PostgreSQL [DHH]
* Change validates_uniqueness_of :case_sensitive option default back to true (from [9160]). Love your database columns, don't LOWER them. [rick]
* Add support for interleaving migrations by storing which migrations have run in the new schema_migrations table. Closes #11493 [jordi]
16 activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -463,6 +463,22 @@ def rename_column(table_name, column_name, new_column_name) #:nodoc:
execute "ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_column_name(column_name)} #{quote_column_name(new_column_name)} #{current_type}"
+ # Maps logical Rails types to MySQL-specific data types.
+ def type_to_sql(type, limit = nil, precision = nil, scale = nil)
+ return super unless type.to_s == 'integer'
+ case limit
+ when 0..3
+ "smallint(#{limit})"
+ when 4..8
+ "int(#{limit})"
+ when 9..20
+ "bigint(#{limit})"
+ else
+ 'int(11)'
+ end
+ end
def show_variable(name)

4 comments on commit a375465


I would replace: :integer => { :name => "int"}, :limit => 11 },

with: :integer => { :name => "int"},

Otherwise, the default case of ‘int(11)’ is never executed.


Even with that change, this is causing problems when cloning the test database, as integer columns that were originally specified without an explicit limit in the migration now have a limit of 11, resulting in them being created as bigint(11) rather than int(11). This in turn breaks the clone when using foreign key constraints.

See [#55] for more details.


This patch is not backwards compatible in general. The default for :integer was int(11), so the new range for “integer” should include 11 (see my meta-patch at


timothynjones, this code is actually going to chance once more with:

And the :limit is no longer going to specify the number of decimal places, instead if will specify the number of bytes, so I’d suggest not at all adding a :limit option to your integers unless you need bigint or mediumint or smallint. For schema.rb there should be no compatibility problems as I believe schema.rb has never dumped :limit for :integer types (rather the change would actually make schema.rb more accurate in being able to create correct integer types where it used to always create 4 byte integers).

Please sign in to comment.
Something went wrong with that request. Please try again.