Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a column of type tinyint(2) or tinyint(3) in Ruby on Rails?

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)?

like image 562
maxedison Avatar asked Jan 30 '14 01:01

maxedison


People also ask

What does Tinyint 3 mean?

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 .

What does Tinyint 2 mean?

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.

What is column in Ruby?

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.


4 Answers

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
like image 198
srbhattarai Avatar answered Oct 18 '22 13:10

srbhattarai


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

like image 44
FedeX Avatar answered Oct 18 '22 12:10

FedeX


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.

like image 36
petkov.np Avatar answered Oct 18 '22 13:10

petkov.np


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.

like image 36
adamszkly Avatar answered Oct 18 '22 12:10

adamszkly