roca
roca

Reputation: 73

Why are Sequelize timestamps not turning off

I can't seem to understand why my Sequelize timestamps are not turning off despite following the documentation notes. I've tried setting this in the model definition and in the Sequelize definition so its database wide.

I try running a findAll query and I keep getting this error:

Executing (default): SELECT 1+1 AS result
Database connection has been established successfully.
Executing (default): SELECT "contact_id", "first_name", 
"last_name","contact_type_id", "org_name", "created_at", 
"updated_at", "createdAt","updatedAt" FROM "contacts" AS 
"contacts";

Unhandled rejection SequelizeDatabaseError: 
column "createdAt" does not exist

Of course it does not exist, I am trying to turn it off.

Anyway, here is my model definition and my Sequelize definition:

contact.js

const Sequelize = require('sequelize');
var Model = Sequelize.Model;

module.exports = (sequelize, DataTypes) => {
  const Contact = sequelize.define('contacts', {
    contact_id: {
      type: Sequelize.INTEGER,
      field: 'contact_id',
      primaryKey: 'true'
    },
    first_name: {
      type: Sequelize.STRING,
      field: 'first_name'
    },
[...] //condensed for legibility
    created_at: {
      type: Sequelize.DATE,
      field: 'created_at'
    },
    updated_at: {
      type: Sequelize.DATE,
      field: 'updated_at'   
    }
  },
  {
  // Options
  tableName: 'contacts',
  timestamps: 'false'
  }
  );

  return Contact;

};

models\index.js

var fs        = require('fs');
var path      = require('path');
var Sequelize = require('sequelize');
var basename  = path.basename(__filename);
var db        = {};

const sequelize = new Sequelize(process.env.DB, process.env.DB_USER,
process.env.DB_PASS, {
  host: xxxxxxxx,
  dialect: 'postgres',
  protocol: 'postgres',
  freezeTableName: 'true'
});

[...] // condensed for legibility

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

Any insight anyone can provide? I've already restart the server a handful of times - both the backend and the front end.

Upvotes: 2

Views: 2658

Answers (1)

Lin Du
Lin Du

Reputation: 102207

timestamps value should be boolean type, not string type. See this docs

  /**
   * Adds createdAt and updatedAt timestamps to the model. Default true.
   */
  timestamps?: boolean;

E.g.

/* eslint-disable @typescript-eslint/camelcase */
import { sequelize } from '../../db';
import Sequelize, { Model } from 'sequelize';

class Contact extends Model {}
Contact.init(
  {
    contact_id: {
      type: Sequelize.INTEGER,
      field: 'contact_id',
      primaryKey: true,
    },
    first_name: {
      type: Sequelize.STRING,
      field: 'first_name',
    },
    created_at: {
      type: Sequelize.DATE,
      field: 'created_at',
    },
    updated_at: {
      type: Sequelize.DATE,
      field: 'updated_at',
    },
  },
  { sequelize, modelName: 'contacts', timestamps: false },
);

(async function test() {
  try {
    await sequelize.sync({ force: true });
    await Contact.findAll();
  } catch (error) {
    console.log(error);
  } finally {
    await sequelize.close();
  }
})();

The execution results:

Executing (default): DROP TABLE IF EXISTS "contacts" CASCADE;
Executing (default): DROP TABLE IF EXISTS "contacts" CASCADE;
Executing (default): CREATE TABLE IF NOT EXISTS "contacts" ("contact_id" INTEGER , "first_name" VARCHAR(255), "created_at" TIMESTAMP WITH TIME ZONE, "updated_at" TIMESTAMP WITH TIME ZONE, PRIMARY KEY ("contact_id"));
Executing (default): SELECT i.relname AS name, ix.indisprimary AS primary, ix.indisunique AS unique, ix.indkey AS indkey, array_agg(a.attnum) as column_indexes, array_agg(a.attname) AS column_names, pg_get_indexdef(ix.indexrelid) AS definition FROM pg_class t, pg_class i, pg_index ix, pg_attribute a WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND t.relkind = 'r' and t.relname = 'contacts' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname;
Executing (default): SELECT "contact_id", "first_name", "created_at", "updated_at" FROM "contacts" AS "contacts";

As you can see, there are no createdAt and updatedAt columns anymore.

Upvotes: 2

Related Questions