NODEJS SEQUELIZE with POSTGRES - fails to create table - getdate() function doesn't exist

83 Views Asked by At

I have a node.js application using sequelize as an ORM, which should create tables if they don't exist on a local postgres development database.

The queries created by sequelize all fail where there are timestamp columns such as 'created' or 'modified' that should take the time a record is created as a default.

The sequelize generated query for create table includes a GETDATE() function for such fields.

Pasting the queries into the PGAdmin tool throws the same error with GETDATE(), as is console logged in node.js.

I can manually change the query in PGAdmin to the NOW() function but this is frustrating given my schema contains multiple tables having such columns.

Is there a fix for this issue?

Below are the versions of sequelize, pg and pg-hstore

"pg": "^8.11.1", "pg-hstore": "^2.3.4" "sequelize": "^6.32.1"

Version of postgres is "PostgreSQL 15.3, compiled by Visual C++ build 1914, 64-bit"

1

There are 1 best solutions below

1
Ismael Martinez On

You can define the model has follows:

    const YourModel = sequelize.define('YourModel', {
      // Other columns...
    
      createdAt: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('NOW()')
      },
    
      updatedAt: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('NOW()')
      }
    }, {
      // Other model options...
    });

Using that way you override the default Sequelize functionality