ArgumentError for invalid :limit & :precision Rails 6

Amit Choudhary avatar

Amit Choudhary

August 27, 2019

This blog is part of our  Rails 6 series.

Rails 6 raises ArgumentError when :limit and :precision are used with invalid datatypes.

Before Rails 6, it used to return ActiveRecord::ActiveRecordError.

Let's checkout how it works.

Rails 5.2

Let's create an orders table and try using :limit with a column named as quantity with data type integer.


> > class CreateOrders < ActiveRecord::Migration[5.2]
> > def change
> > create_table :orders do |t|
> > t.string :item
> > t.integer :quantity, limit: 10
> >
> >       t.timestamps
> >     end
> >
> > end
> > end

=> :change

> > CreateOrders.new.change
> > -- create_table(:orders)

=> Traceback (most recent call last):
2: from (irb):11
1: from (irb):3:in 'change'
ActiveRecord::ActiveRecordError (No integer type has byte size 10. Use a numeric with scale 0 instead.)

We can see that use of :limit with integer column raises ActiveRecord::ActiveRecordError in Rails 5.2.

Now let's try using :precision of 10 with a datetime column.


> > class CreateOrders < ActiveRecord::Migration[5.2]
> > def change
> > create_table :orders do |t|
> > t.string :item
> > t.integer :quantity
> > t.datetime :completed_at, precision: 10
> >
> >       t.timestamps
> >     end
> >
> > end
> > end

=> :change

> > CreateOrders.new.change
> > -- create_table(:orders)

=> Traceback (most recent call last):
2: from (irb):12
1: from (irb):3:in 'change'
ActiveRecord::ActiveRecordError (No timestamp type has precision of 10. The allowed range of precision is from 0 to 6)

We can see that invalid value of :precision with datetime column also raises ActiveRecord::ActiveRecordError in Rails 5.2.

Rails 6.0.0.rc1

Let's create an orders table and try using :limit with a column named as quantity with data type integer in Rails 6.


> > class CreateOrders < ActiveRecord::Migration[6.0]
> > def change
> > create_table :orders do |t|
> > t.string :item
> > t.integer :quantity, limit: 10
> >
> >       t.timestamps
> >     end
> >
> > end
> > end

=> :change

> > CreateOrders.new.change
> > -- create_table(:orders)

=> Traceback (most recent call last):
2: from (irb):11
1: from (irb):3:in 'change'
ArgumentError (No integer type has byte size 10. Use a numeric with scale 0 instead.)

We can see that use of :limit with integer column raises ArgumentError in Rails 6.

Now let's try using :precision of 10 with a datetime column.


> > class CreateOrders < ActiveRecord::Migration[6.0]
> > def change
> > create_table :orders do |t|
> > t.string :item
> > t.integer :quantity
> > t.datetime :completed_at, precision: 10
> >
> >       t.timestamps
> >     end
> >
> > end
> > end

=> :change

> > CreateOrders.new.change
> > -- create_table(:orders)

=> Traceback (most recent call last):
2: from (irb):12
1: from (irb):3:in 'change'
ArgumentError (No timestamp type has precision of 10. The allowed range of precision is from 0 to 6)

We can see that invalid value of :precision with datetime column also raises ArgumentError in Rails 6.

Here is the relevant pull request.

Follow @bigbinary on X. Check out our full blog archive.