15

I am creating a table using Knex.JS, and the table has a column for a currency value.

For example, here is the column amount:

knex.schema.createTable('payment', function(table) {
  table.increments();
  table.float('amount');
})

Currently, I am using the float type, but I'd like to use the numeric type. What is the equivalent of the numeric type in Knex.JS?

Thanks.

Jonah
  • 15,806
  • 22
  • 87
  • 161
aashah7
  • 2,075
  • 1
  • 17
  • 24

1 Answers1

16

For currency decimal is best match, so your code may look like:

knex.schema.createTable('payment', function(table) {
  table.increments();
  table.decimal('amount',14,2); // e.g. 14 positions, 2 for the cents
});

see http://knexjs.org/#Schema-decimal

flaviodesousa
  • 7,255
  • 4
  • 28
  • 34
  • 1
    I was confused about this as well. From the [postgres docs](https://www.postgresql.org/docs/12/datatype-numeric.html): "The types `decimal` and `numeric` are equivalent. Both types are part of the SQL standard." – Matt Sanders Oct 15 '19 at 18:39