Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Decimal column with zero scale dumped as integer column

#2741

If you declare a decimal column with a scale of 0 in a migration:

create_table :large_integers do |t|
  t.decimal :large, :scale => 0, :precision => 80

  t.timestamps
end

Then a table will be created like the following:

large      | numeric(40,0)               |
created_at | timestamp without time zone | 
updated_at | timestamp without time zone |

But, after running rake db:schema:dump, db/schema.rb will contain something like the following:

create_table "large_integers", :force => true do |t|
  t.integer  "large",      :limit => 80, :precision => 80, :scale => 0
  t.datetime "created_at"
  t.datetime "updated_at"
end

On PostgreSQL, this will create an integer column, not a numeric column, and if the precision is large then the column can not hold numbers up to that precision.

Plus, it is a little surprising to have schema.rb differing from your migration like this.

The attached patch fixes this problem in the schema dumper by looking for integer columns where the underlying sql_type is decimal or numeric, and then setting the type to be emitted to 'decimal' instead of 'integer'.

Reported by Giles Alexander · May 30th, 2009 @ 09:13 AM

State: committed
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Michael Koziarski
    Michael Koziarski

    Isn't the real problem here that column.type for that column is returning integer rather than decimal?

    The fix should probably be there rather than just in the dumper?

    June 1st, 2009 @ 01:11 AM

  2. Giles Alexander
    Giles Alexander

    I tried that, and it caused a lot of test failures. There's an optimisation in ActiveRecord to represent decimal columns with a scale of 0 as integers. This is a pretty good optimisation, that I actually want, and it's described in the documentation.

    This just changes the dumper to notice when that optimisation has been applied.

    June 1st, 2009 @ 01:42 AM

  3. Repository
    Repository
    • State changed from new to committed

    (from [532219fd091837a9312a301c74e0fbf06abab3a8]) Schema dumper now records scale 0 decimal columns as decimal not integer.

    The schema dumper would dump out any decimal or numeric column that had a zero
    scale as an integer column. This will cause problems for very large precision
    columns on some DBMSs, particularly PostgreSQL. It also looks strange to see
    your column change type after moving through schema.rb.

    Signed-off-by: Michael Koziarski michael@koziarski.com
    [#2741 Decimal column with zero scale dumped as integer column state:committed] http://github.com/rails/rails/commit/532219fd091837a9312a301c74e0fb...

    June 1st, 2009 @ 02:40 AM

  4. Michael Koziarski
    Michael Koziarski

    I had to change the precision down to 55 in the tests as mysql (at least on my mac) doesn't support precisions that high.

    Does it still look ok?

    June 1st, 2009 @ 02:45 AM

  5. Giles Alexander
    Giles Alexander

    Thanks. Yes, that still looks ok.

    June 1st, 2009 @ 02:47 AM