How to create a column of type tinyint(2) or tinyint(3) in Ruby on Rails?
For tinyint(2)
create_table :great_table do |t| t.integer :step_position, :limit => 2end
For tinyint(3)
create_table :great_table do |t| t.integer :step_position, :limit => 3end
According to what I can see in the source code of the gem, you can't:
# File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 540 540: def type_to_sql(type, limit = nil, precision = nil, scale = nil) 541: return super unless type.to_s == 'integer' 542: 543: case limit 544: when 1; 'tinyint' 545: when 2; 'smallint' 546: when 3; 'mediumint' 547: when nil, 4, 11; 'int(11)' # compatibility with MySQL default 548: when 5..8; 'bigint' 549: else raise(ActiveRecordError, "No integer type has byte size #{limit}") 550: end 551: end
There is no such thing as tinyint(4)
in MySQL in the first place. tinyint
is a one byte signed integer. You can check all integer types in the docs. You may see something like tinyint(1)
even in the Rails source code, but I think it's a tautology as tinyint
already implies one byte storage.
The Rails way to declare TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT
in a migration is by using limit:
with the appropriate byte size as can be seen in the source code.
Beware that Rails will treat one-byte integers as booleans by default though, as can be seen from the above link.