Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize decimal data save with 2 decimal points

I'm using sequlize ORM for my Node.js project. Below code is one column in a table.

   itemPrice: {
      type: DataTypes.DECIMAL,
      allowNull: false,
      field: 'itemPrice'
    },

It is generate the MYSQL DB column as decimal(10,0). It means it cannot save decimal points data. When I'm going to save 12.26, it is save 12. How to create column with saving 2 decimal points.

I tried below code also. It doesn't execute and occurred an error.

   itemPrice: {
      type: DataTypes.DECIMAL(10,2),
      allowNull: false,
      field: 'itemPrice'
    },

Please show me a direction to do this...

like image 648
weeraa Avatar asked Mar 06 '23 05:03

weeraa


1 Answers

type: DataTypes.DECIMAL(10,2), should work. Once a table is created via sync, it will not be altered even though you have changed your data model definition.

You can use {force: true} in your sync function. Remember that this will drop your table and recreate it.

sequelize.sync({ force: true })
  .then(()
like image 136
AbhinavD Avatar answered Mar 14 '23 22:03

AbhinavD