Skip to content

BigDecimal returned from SELECT on decimal column results in value 0 on INSERT #714

Closed
@lhz

Description

@lhz

I have a script to copy rows from a table in one database to an identical table in another. The table has 4 columns, one of which is of type decimal(15, 0).

The code to copy a batch of rows looks like this:

  select = @db1.prepare('SELECT * FROM my_table WHERE id BETWEEN ? AND ? LIMIT ?')
  insert = @db2.prepare('INSERT INTO my_table VALUES (?, ?, ?, ?)')
  select.execute(min_id, max_id, batch_size).each do |row|
    insert.execute *row.values
  end

The value of the decimal column will be returned as a BigDecimal object. When including this object in the arguments passed to exectue on the prepared insert statement, this always results in the value 0 for that column in the row inserted, regardless of the actual value.

I can circumvent the issue by casting the BigDecimal to Integer before insertion (or by changing the column type to integer, but I don't have the luxury of changing the schema in this case). Still, it seems contraintuitive that you can not directly insert the same value you got out of a select on the same column type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions