Skip to content

Exception when adding expense with PostgreSQL #538

@elad-eyal

Description

@elad-eyal

trying to feed expense value 123.456 I get an exception

ActiveRecord::RangeError (PG::NumericValueOutOfRange: ERROR:  numeric field overflow                                                                                                                         
DETAIL:  A field with precision 6, scale 4 must round to an absolute value less than 10^2.                                                        : 

INSERT INTO "expenses" ("name", "value", "reimbursed", "person_id", "conference_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"):                                                           

app/controllers/expenses_controller.rb:30:in `create'

@person.expenses << e

The same seems to work OK with sqlite3.


I think there are two issues here.

(1) The migration to add the expenses wanted it precision: 7, scale: 2 so the max value is ¤100,000 which makes sense.

change_column :expenses, :value, :decimal, precision: 7, scale: 2

but db/schema.rb has precision: 6, scale: 4 which has a greater granularity for some reason but bound by ¤100.
t.decimal "value", precision: 6, scale: 4

(2) why does an invalid value trigger an exception rather than an error message presented to the user?

I guess the first problem can be fixed by a PR to db/schema.rb plus a migration; and the second problem can be fixed by a Rails validates in https://github.com/frab/frab/blob/master/app/models/expense.rb.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions