Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createdAt / updateAt in table; missing in Model with timestamps = true; Postgres #650

Open
mwbeckner opened this issue May 25, 2023 · 1 comment

Comments

@mwbeckner
Copy link

Given a tables with created_at and updated_at defined --

CREATE TABLE scan_forms (
    id character varying(255) PRIMARY KEY,
    customer_id integer,
    pickup_address_id integer,
    customer_reference character varying(255),
    package_count integer,
    pickup_associate character varying(255),
    pickup_time character varying(255),
    ship_date timestamp without time zone NOT NULL,
    created_at timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
    total_weight numeric(6,2)
);

Generating the model with timestamps: true results in missing createdAt / updatedAt attributes --

// Model generated with timestamps: true
const Sequelize = require('sequelize');
module.exports = function(sequelize, DataTypes) {
  return sequelize.define('ScanForm', {
    id: {
      type: DataTypes.STRING(255),
      allowNull: false,
      primaryKey: true
    },
    customerId: {
      type: DataTypes.INTEGER,
      allowNull: true,
      field: 'customer_id'
    },
    pickupAddressId: {
      type: DataTypes.INTEGER,
      allowNull: true,
      field: 'pickup_address_id'
    },
    customerReference: {
      type: DataTypes.STRING(255),
      allowNull: true,
      field: 'customer_reference'
    },
    packageCount: {
      type: DataTypes.INTEGER,
      allowNull: true,
      field: 'package_count'
    },
    pickupAssociate: {
      type: DataTypes.STRING(255),
      allowNull: true,
      field: 'pickup_associate'
    },
    pickupTime: {
      type: DataTypes.STRING(255),
      allowNull: true,
      field: 'pickup_time'
    },
    shipDate: {
      type: DataTypes.DATE,
      allowNull: false,
      field: 'ship_date'
    },
    totalWeight: {
      type: DataTypes.DECIMAL,
      allowNull: true,
      field: 'total_weight'
    }
  }, {
    sequelize,
    tableName: 'scan_forms',
    schema: 'public',
    timestamps: true,
    indexes: [
      {
        name: "scan_forms_pkey",
        unique: true,
        fields: [
          { name: "id" },
        ]
      },
    ]
  });
};

Generating with timestamps: false results in generated createdAt / updatedAt attributes, but disabled for use by Sequelize --

// Model generated with timestamps: false
const Sequelize = require('sequelize');
module.exports = function(sequelize, DataTypes) {
  return sequelize.define('ScanForm', {
    id: {
      type: DataTypes.STRING(255),
      allowNull: false,
      primaryKey: true
    },
    customerId: {
      type: DataTypes.INTEGER,
      allowNull: true,
      field: 'customer_id'
    },
    pickupAddressId: {
      type: DataTypes.INTEGER,
      allowNull: true,
      field: 'pickup_address_id'
    },
    customerReference: {
      type: DataTypes.STRING(255),
      allowNull: true,
      field: 'customer_reference'
    },
    packageCount: {
      type: DataTypes.INTEGER,
      allowNull: true,
      field: 'package_count'
    },
    pickupAssociate: {
      type: DataTypes.STRING(255),
      allowNull: true,
      field: 'pickup_associate'
    },
    pickupTime: {
      type: DataTypes.STRING(255),
      allowNull: true,
      field: 'pickup_time'
    },
    shipDate: {
      type: DataTypes.DATE,
      allowNull: false,
      field: 'ship_date'
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: Sequelize.Sequelize.literal('CURRENT_TIMESTAMP'),
      field: 'created_at'
    },
    updatedAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: Sequelize.Sequelize.literal('CURRENT_TIMESTAMP'),
      field: 'updated_at'
    },
    totalWeight: {
      type: DataTypes.DECIMAL,
      allowNull: true,
      field: 'total_weight'
    }
  }, {
    sequelize,
    tableName: 'scan_forms',
    schema: 'public',
    timestamps: false,
    indexes: [
      {
        name: "scan_forms_pkey",
        unique: true,
        fields: [
          { name: "id" },
        ]
      },
    ]
  });
};

My SequelizeAuto config is --

{
      host: 'localhost',
      dialect: 'postgres',
      port: '5432',
      directory: `./updated-models/${sourceDbName}`, // where to write files
      views: true,
      caseModel: 'p', // convert snake_case column names to PascalCase field names: user_id -> userId
      caseFile: 'k', // file names created for each model use kabob-case.js not snake_case.js
      caseProp: 'c', // file names created for each model use camelCase.js not snake_case.js
      singularize: true, // convert plural table names to singular model names
      additional: { // ...options added to each model
        timestamps: false // already config'd
      },
      tables: newTables,
      noInitModels: true
}
@mwbeckner
Copy link
Author

My expectation is that Models should be generated to include all table fields/columns; timestamp setting should not affect that. CreatedAt / updatedAt should always be generated and the model option for timestamps be set from the generation config.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant