Blog

Thoughts from my daily grind

List of Rails data types for ActiveRecord migrations

Posted by Ziyan Junaideen |Published: 22 October 2021 |Category: Ruby on Rails
Default Upload |

I started on Ruby on Rails eight years ago. Ever since ActiveRecord has remained one of my favourite features. ActiveRecord made interacting with the database intuitive but at the same time a little confusing. Before Rails & ActiveRecord, I would usually write SQL to create and migrate databases. I was very good with SQL, but suddenly, ActiveRecord made me relearn things. Bjoern, a German CTO, pointed me to the PostgreSQL Adapter as a convenient starting point.

Common Data Types

Representation PostgreSQL sqlite3 MySQL Oracle SQLSerer
:binary bytea blob blob blob image
:boolean boolean boolean tinyint(1) number(1) bit
:date date date date date date
:datetime timestamp datetime datetime datetime datetime
:decimal decimal decimal decimal decimal decimal
:float float float float number float(8)
:integer integer integer int(11) number(38) int
:bigint bigint
:string character varying varchar(255) var-char(255) varchar2(255) varchar(255)
:text text text text clob text
:time datetime datetime time date time
:timestamp timestamp datetime datetime date datetime

Let's use this in an imaginary model.

class CreateUsers < ActiveRecord::Migration[6.1]
  def change
    create_table :users do |t|
      t.string :name
      t.date :birthday
      t.integer :weight, limit: 2. # in grams
      t.text :description
      t.decimal :height, precision: 4, scale: 2 # aliens could be taller than 10 feet 🧐
      t.timestamps
    end
  end
end

Integers

Integers come in a variety of flavours, w.r.t. the number of bytes. This can be controlled by providing the limit option in the migration.

limit PostgreSQL MySQL Size
(bytes)
Max
1 tinyint 1 127
2 smallint/int2 smallint 2 32767
3 mediumint 3 8388607
4 (default) integer/int/int4 int 4 2147483647
5 to 8 bigint/int8 bigint 8 9223372036854775807

Coming into an understanding that a customer will not place an order for 30k items, we can have a migration to add quantity as a 2-byte integer (with an upper limit of 32767) to line items as follows:

class AddQuantityToLineItems < ActiveRecord::Migration[6.1]
  def change
    add_column :line_items, :quantity, :integer, limit: 2, default: 0
  end
end

PostgreSQL Specific Data Types

By far, PostgreSQL is the sweetheart (or sweet-database) of the Ruby on Rails community, and for a good reason. There are some PG-specific data types no Rails developer should miss.

Representation PG Type Description
:hstore hstore an indexed jsonb might be better?
:json json the same json every one loves
:jsonb jsonb binary format
:array array have used in query optimizations
:inet inet IPv4 IPv6 host addresses
:cidr cidr IPv4 IPv6 network address
:macaddr macaddr Mac address

You can see a full list of supported data types from the adapter source code here. My favourite of the bunch are json and jsonb, and I often use PG somewhat similar to a document database.

Conclusion

Both seasoned Rubyists and newbies to Ruby occasionally need to refer to data types supported by Rails and ActiveRecord. I hope this blog post was of help. I rarely use MySQL, SQL Server and Oracle databases. If any of these data are wrong or out of date, feel free to comment below.

Tags
About the Author

Ziyan Junaideen -

Ziyan is an expert Ruby on Rails web developer with 8 years of experience specializing in SaaS applications. He spends his free time he writes blogs, drawing on his iPad, shoots photos.

Comments