Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

SQLite3 Decimals being inserted as blobs #2162

Closed
justindell opened this Issue · 5 comments

2 participants

@justindell

Decimals seem to be inserted incorrectly as blobs in sqlite3, and as a result I am unable to do > or < comparisons on decimals. I have reproduced it by doing the following:

rails new decimal_bug
cd decimal_bug/
rails g scaffold my_number amount:decimal

I then created a "my_number" through the UI with an amount of 100.0. After firing up the dbconsole I executed the following:

select *, typeof(amount) from my_numbers where amount > 200;
1|100.0|2011-07-20 01:58:13.119675|2011-07-20 01:58:13.119675|blob

I added a line to the active_record/test/cases/base_test.rb test_numeric_fields test that reproduces the issue:

def test_numeric_fields
m = NumericData.new(
:bank_balance => 1586.43,
:big_bank_balance => BigDecimal("1000234000567.95"),
:world_population => 6000000000,
:my_house_population => 3
)

assert m.save

...

assert_equal 0, NumericData.where("bank_balance > ?", 2000.0).count
end

@tenderlove
Owner

Can you post the create statement that is generated for your table? Also post the select statement for that is being generated?

Thanks!

@justindell

Here's the create statement from the log:

SQL (0.5ms) INSERT INTO "my_numbers" ("amount", "created_at", "updated_at") VALUES (?, ?, ?) [["amount", #<BigDecimal:10306ac30,'0.5E3',9(18)>], ["created_at", Wed, 20 Jul 2011 01:58:23 UTC +00:00], ["updated_at", Wed, 20 Jul 2011 01:58:23 UTC +00:00]]

The select statement I would like to do is something along the lines of:
SELECT COUNT(*) FROM "my_numbers" WHERE (amount > 200.0)
which is generated by MyNumber.where("amount > ?", 200.0).count

@tenderlove
Owner

I mean, can you post the create table statement?

@justindell

CREATE TABLE "my_numbers" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "amount" decimal, "created_at" datetime, "updated_at" datetime)

@tenderlove tenderlove was assigned
@justindell

It's worth noting that this is on the master branch. Rails 3.0.9 does not have this issue since it is storing the amounts as either integers or reals, not blobs.

@lsylvester lsylvester referenced this issue from a commit in lsylvester/rails
@al2o3cr al2o3cr Fix several issues with code examples.
- "chaining" layouts with :only/:except removed. See #2162 for discussion.

- double render section now has working examples. Old version implied that calling render once and then falling through would give a DoubleRenderError.

- complete example for layout proc.

- cleaned up a few stray references to layouts ending in bare .erb (no format).
4f7e225
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.