This project is archived and is in readonly mode.
Decimal column with zero scale dumped as integer column
Reported by Giles Alexander | May 30th, 2009 @ 09:13 AM | in 2.x
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'.
Comments and changes to this ticket
-
Michael Koziarski June 1st, 2009 @ 01:11 AM
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?
-
Giles Alexander June 1st, 2009 @ 01:42 AM
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.
-
Repository June 1st, 2009 @ 02:40 AM
- 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 state:committed] http://github.com/rails/rails/commit/532219fd091837a9312a301c74e0fb... -
Michael Koziarski June 1st, 2009 @ 02:45 AM
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?
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Referenced by
- 2741 Decimal column with zero scale dumped as integer column Signed-off-by: Michael Koziarski michael@koziarski.com [#...