Back to Blog

ArgumentError for invalid :limit & :precision Rails 6

on August 27, 2019
This blog is part of our Rails 6 series.

Rails 6.0 was recently released.

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.

1
2> > class CreateOrders < ActiveRecord::Migration[5.2]
3> > def change
4> > create_table :orders do |t|
5> > t.string :item
6> > t.integer :quantity, limit: 10
7> >
8> >       t.timestamps
9> >     end
10> >
11> > end
12> > end
13
14=> :change
15
16> > CreateOrders.new.change
17> > -- create_table(:orders)
18
19=> Traceback (most recent call last):
202: from (irb):11
211: from (irb):3:in 'change'
22ActiveRecord::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.

1
2> > class CreateOrders < ActiveRecord::Migration[5.2]
3> > def change
4> > create_table :orders do |t|
5> > t.string :item
6> > t.integer :quantity
7> > t.datetime :completed_at, precision: 10
8> >
9> >       t.timestamps
10> >     end
11> >
12> > end
13> > end
14
15=> :change
16
17> > CreateOrders.new.change
18> > -- create_table(:orders)
19
20=> Traceback (most recent call last):
212: from (irb):12
221: from (irb):3:in 'change'
23ActiveRecord::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.

1
2> > class CreateOrders < ActiveRecord::Migration[6.0]
3> > def change
4> > create_table :orders do |t|
5> > t.string :item
6> > t.integer :quantity, limit: 10
7> >
8> >       t.timestamps
9> >     end
10> >
11> > end
12> > end
13
14=> :change
15
16> > CreateOrders.new.change
17> > -- create_table(:orders)
18
19=> Traceback (most recent call last):
202: from (irb):11
211: from (irb):3:in 'change'
22ArgumentError (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.

1
2> > class CreateOrders < ActiveRecord::Migration[6.0]
3> > def change
4> > create_table :orders do |t|
5> > t.string :item
6> > t.integer :quantity
7> > t.datetime :completed_at, precision: 10
8> >
9> >       t.timestamps
10> >     end
11> >
12> > end
13> > end
14
15=> :change
16
17> > CreateOrders.new.change
18> > -- create_table(:orders)
19
20=> Traceback (most recent call last):
212: from (irb):12
221: from (irb):3:in 'change'
23ArgumentError (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.


You might also like

If you liked this blog post, check out similar ones from BigBinary