In Ruby on Rails, the following code in a migration creates a column of type tinyint(4)
in MySQL:
create_table :great_table do |t|
t.integer :step_position, :limit => 1 #tinyint
end
How would I create a column of type tinyint(2)
or tinyint(3)
?
This answer is not useful. Show activity on this post. Data-wise, tinyint(1) , tinyint(2) , tinyint(3) etc. are all exactly the same. They are all in the range -128 to 127 for SIGNED or 0-255 for UNSIGNED .
The number 2 and 1 in TINYINT(2) vs TINYINT(1) indicate the display width. There is no difference between tinyint(1) and tinyint(2) except the width. If you use tinyint(2) or even tinyint(1), the difference is the same. You can understand the above concept using zerofill option.
The column() is an inbuilt method in Ruby returns a vector that has all the elements in the column number col_num. Syntax: mat1.column(col_num) Parameters: The function accepts a parameter col_num which is the column number. Return Value: It returns a vector which has all the elements of column col_num.
For tinyint(2)
create_table :great_table do |t|
t.integer :step_position, :limit => 2
end
For tinyint(3)
create_table :great_table do |t|
t.integer :step_position, :limit => 3
end
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
type_to_sql
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.
You can use :tinyint
as the column type in a rails migration, and write limit: 2
or limit: 3
as written before.
Using your example:
create_table :great_table do |t|
t.tinyint :step_position, :limit => 1
end
should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With