// package.json "pg": "^8.5.1", "sequelize": "^6.5.0", "sequelize-cli": "^6.2.0" // terminal $ node --version v14.15.4
This is the entire migration file I see after running npx sequelize db:migrate
.
'use strict'; module.exports = { up: async (queryInterface, Sequelize) => { await queryInterface.createTable('Documents', { id: { allowNull: false, autoIncrement: true, primaryKey: true, type: Sequelize.INTEGER }, id: { type: Sequelize.INTEGER }, createdAt: { allowNull: false, type: Sequelize.DATE }, updatedAt: { allowNull: false, type: Sequelize.DATE }, }); }, down: async (queryInterface, Sequelize) => { await queryInterface.dropTable('Documents'); } };
Database table inspection using DBeaver shows only one id
column, not the duplicate seen in the migration file. Two other columns that are always added as well. Everything else is missing. https://i.imgur.com/FvsyAAN.png
Under ./models
there is only one file document.js
, also created by the CLI. I have updated it to this:
'use strict'; const { Model } = require('sequelize'); module.exports = (sequelize, DataTypes) => { class Document extends Model { static associate(models) { } }; Document.init({ id: { type: DataTypes.uuid4, defaultValue: DataTypes.uuid4, primaryKey: true, allowNull: false, }, ownerDoc: { type: DataTypes.String, allowNull: false, }, ownerName: { type: DataTypes.String, allowNull: false, }, createdAt: DataTypes.Date, updatedAt: DataTypes.Date, deletdAt: { type: DataTypes.Date, allowNull: true, }, uploadBy: { type: DataTypes.String, allowNull: true, }, fileUrl: { type: DataTypes.String, allowNull: true, }, category: { type: DataTypes.String, allowNull: false, }, status: { type: DataTypes.String, allowNull: false, }, fileType: { type: DataTypes.String, allowNull: true, }, version: { type: DataTypes.number, allowNull: false, }, type: { type: DataTypes.String, allowNull: false, }, source: { type: DataTypes.String, allowNull: false, }, data: { type: DataTypes.JSON, allowNull: true, }, // any[], expiration: { type: DataTypes.Date, allowNull: true, }, requestAgainExpiration: { type: DataTypes.number, allowNull: true, }, titleSufix: { type: DataTypes.String, allowNull: true, }, inputRequest: { type: DataTypes.JSON, allowNull: true, }, // any[], parentId: { type: DataTypes.String, allowNull: true, }, borrowerId: { type: DataTypes.String, allowNull: true, }, loanApplicationId: { type: DataTypes.String, allowNull: true, }, }, { sequelize, modelName: 'Document', }); return Document; };
Here is the CLI output
[email protected]:~/cp/caBackend$ npx sequelize db:migrate Sequelize CLI [Node: 14.15.4, CLI: 6.2.0, ORM: 6.5.0] Loaded configuration file "config/config.js". Using environment "development". No migrations were executed, database schema was already up to date. [email protected]:~/cp/caBackend$ npx sequelize db:migrate Sequelize CLI [Node: 14.15.4, CLI: 6.2.0, ORM: 6.5.0] Loaded configuration file "config/config.js". Using environment "development". No migrations were executed, database schema was already up to date. [email protected]:~/cp/caBackend$
Is anyone able to tell why the migration is failing to update correctly from the model file?
Tried
// migration file, same one as shown above 'use strict'; module.exports = { up: (queryInterface, Sequelize) => Promise.all([ queryInterface.addColumn('Documents', 'ownerDoc', { type: Sequelize.String, allowNull: false }), queryInterface.addColumn('Documents', 'ownerName', { type: Sequelize.String, allowNull: false }), queryInterface.addColumn('Documents', 'deletdAt', { type: Sequelize.DATE, allowNull: true }), queryInterface.addColumn('Documents', 'deletdAt', { type: Sequelize.DATE, allowNull: true }), queryInterface.addColumn('Documents', 'uploadBy', { type: Sequelize.STRING, allowNull: true }), queryInterface.addColumn('Documents', 'fileUrl', { type: Sequelize.STRING, allowNull: true }), queryInterface.addColumn('Documents', 'category', { type: Sequelize.STRING, allowNull: false }), queryInterface.addColumn('Documents', 'status', { type: Sequelize.STRING, allowNull: false }), queryInterface.addColumn('Documents', 'fileType', { type: Sequelize.STRING, allowNull: true }), queryInterface.addColumn('Documents', 'version', { type: Sequelize.STRING, allowNull: true }), queryInterface.addColumn('Documents', 'type', { type: Sequelize.STRING, allowNull: false }), queryInterface.addColumn('Documents', 'source', { type: Sequelize.STRING, allowNull: false }), queryInterface.addColumn('Documents', 'data', { type: Sequelize.JSON, allowNull: true }), queryInterface.addColumn('Documents', 'expiration', { type: Sequelize.Date, allowNull: true }), queryInterface.addColumn('Documents', 'requestAgainExpiration', { type: Sequelize.NUMBER, allowNull: true }), queryInterface.addColumn('Documents', 'titleSufix', { type: Sequelize.STRING, allowNull: true }), queryInterface.addColumn('Documents', 'inputRequest', { type: Sequelize.JSON, allowNull: false }), queryInterface.addColumn('Documents', 'parentId', { type: Sequelize.STRING, allowNull: true }), queryInterface.addColumn('Documents', 'borrowerId', { type: Sequelize.STRING, allowNull: true }), queryInterface.addColumn('Documents', 'loanApplicationId', { type: Sequelize.STRING, allowNull: true }), ]), down: async (queryInterface, Sequelize) => { await queryInterface.dropTable('Documents'); }, };
Both npx sequelize db:migrate
and npx sequelize-cli db:migrate
show:
No migrations were executed, database schema was already up to date.
[email protected]:~/cp/caBackend$ npx sequelize migration:create –name alter-documetns
'use strict'; module.exports = { up: (queryInterface, Sequelize) => { return Promise.all([ queryInterface.addColumn('Documents', 'ownerDoc', { type: DataTypes.String, allowNull: false, }), queryInterface.addColumn('Documents', 'ownerName', { type: DataTypes.String, allowNull: false, }), queryInterface.addColumn('Documents', 'deletdAt', { type: DataTypes.DATE, allowNull: true}), queryInterface.addColumn('Documents', 'deletdAt', { type: DataTypes.DATE, allowNull: true}), queryInterface.addColumn('Documents', 'uploadBy', { type: DataTypes.STRING, allowNull: true}), queryInterface.addColumn('Documents', 'fileUrl', { type: DataTypes.STRING, allowNull: true}), queryInterface.addColumn('Documents', 'category', { type: DataTypes.STRING, allowNull: false}), queryInterface.addColumn('Documents', 'status', { type: DataTypes.STRING, allowNull: false}), queryInterface.addColumn('Documents', 'fileType', { type: DataTypes.STRING, allowNull: true}), queryInterface.addColumn('Documents', 'version', { type: DataTypes.STRING, allowNull: true}), queryInterface.addColumn('Documents', 'type', { type: DataTypes.STRING, allowNull: false}), queryInterface.addColumn('Documents', 'source', { type: DataTypes.STRING, allowNull: false}), queryInterface.addColumn('Documents', 'data', { type: DataTypes.JSON, allowNull: true}), queryInterface.addColumn('Documents', 'expiration', { type: DataTypes.Date, allowNull: true}), queryInterface.addColumn('Documents', 'requestAgainExpiration', { type: DataTypes.NUMBER, allowNull: true}), queryInterface.addColumn('Documents', 'titleSufix', { type: DataTypes.STRING, allowNull: true}), queryInterface.addColumn('Documents', 'inputRequest', { type: DataTypes.JSON, allowNull: false}), queryInterface.addColumn('Documents', 'parentId', { type: DataTypes.STRING, allowNull: true}), queryInterface.addColumn('Documents', 'borrowerId', { type: DataTypes.STRING, allowNull: true}), queryInterface.addColumn('Documents', 'loanApplicationId', { type: DataTypes.STRING, allowNull: true}), ]) }, down: (queryInterface, Sequelize) => { } };
npx sequelize migration:create –name alter-documents
And then replacing default with above code:
$ npx sequelize db:migrate Sequelize CLI [Node: 14.15.4, CLI: 6.2.0, ORM: 6.5.0] Loaded configuration file "config/config.js". Using environment "development". == 20210306190146-alter-documents: migrating ======= ERROR: Cannot read property 'toString' of undefined
Answer
[email protected]:~/cp/caBackend$ npx sequelize migration:create –name alter-documetns
'use strict'; module.exports = { up: (queryInterface, DataTypes) => { return Promise.all([ queryInterface.addColumn('Documents', 'ownerDoc', { type: DataTypes.String, allowNull: false, }), queryInterface.addColumn('Documents', 'ownerName', { type: DataTypes.String, allowNull: false, }), queryInterface.addColumn('Documents', 'deletdAt', { type: DataTypes.DATE, allowNull: true}), queryInterface.addColumn('Documents', 'deletdAt', { type: DataTypes.DATE, allowNull: true}), queryInterface.addColumn('Documents', 'uploadBy', { type: DataTypes.STRING, allowNull: true}), queryInterface.addColumn('Documents', 'fileUrl', { type: DataTypes.STRING, allowNull: true}), queryInterface.addColumn('Documents', 'category', { type: DataTypes.STRING, allowNull: false}), queryInterface.addColumn('Documents', 'status', { type: DataTypes.STRING, allowNull: false}), queryInterface.addColumn('Documents', 'fileType', { type: DataTypes.STRING, allowNull: true}), queryInterface.addColumn('Documents', 'version', { type: DataTypes.STRING, allowNull: true}), queryInterface.addColumn('Documents', 'type', { type: DataTypes.STRING, allowNull: false}), queryInterface.addColumn('Documents', 'source', { type: DataTypes.STRING, allowNull: false}), queryInterface.addColumn('Documents', 'data', { type: DataTypes.JSON, allowNull: true}), queryInterface.addColumn('Documents', 'expiration', { type: DataTypes.Date, allowNull: true}), queryInterface.addColumn('Documents', 'requestAgainExpiration', { type: DataTypes.NUMBER, allowNull: true}), queryInterface.addColumn('Documents', 'titleSufix', { type: DataTypes.STRING, allowNull: true}), queryInterface.addColumn('Documents', 'inputRequest', { type: DataTypes.JSON, allowNull: false}), queryInterface.addColumn('Documents', 'parentId', { type: DataTypes.STRING, allowNull: true}), queryInterface.addColumn('Documents', 'borrowerId', { type: DataTypes.STRING, allowNull: true}), queryInterface.addColumn('Documents', 'loanApplicationId', { type: DataTypes.STRING, allowNull: true}), ]) }, down: (queryInterface, Sequelize) => { } };
npx sequelize migration:create –name alter-documents
And then replacing default with above code:
$ npx sequelize db:migrate Sequelize CLI [Node: 14.15.4, CLI: 6.2.0, ORM: 6.5.0] Loaded configuration file "config/config.js". Using environment "development". == 20210306190146-alter-documents: migrating ======= ERROR: Cannot read property 'toString' of undefined