I have run this command to generate the models from my MySQL:
node node_modules/sequelize-auto/bin/sequelize-auto -h localhost -d coasteye_new -u root -x PASSWORD --dialect mysql -o models
So the models are now stored in the "node_project/models" directory. For example this one:
const Sequelize = require('sequelize');
module.exports = function(sequelize, DataTypes) {
return sequelize.define('user', {
id: {
autoIncrement: true,
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true
},
name: {
type: DataTypes.STRING(100),
allowNull: true,
unique: "name"
},
password: {
type: DataTypes.STRING(100),
allowNull: false
},
id_role: {
type: DataTypes.INTEGER,
allowNull: false
},
role: {
type: DataTypes.ENUM('root','twm staff','client'),
allowNull: false
},
email: {
type: DataTypes.STRING(70),
allowNull: false,
unique: "unique_email"
},
fullname: {
type: DataTypes.STRING(100),
allowNull: true
},
activated: {
type: DataTypes.TINYINT,
allowNull: false,
defaultValue: 1
},
token: {
type: DataTypes.STRING(100),
allowNull: false
},
phone: {
type: DataTypes.STRING(70),
allowNull: false
},
api_token: {
type: DataTypes.STRING(200),
allowNull: true
},
api_token_valid_until: {
type: DataTypes.DATE,
allowNull: true
},
has_stations_in_the_new_user_deployment_xref_table: {
type: DataTypes.TINYINT,
allowNull: false,
defaultValue: 0
},
send_the_status_daily_report_by_email: {
type: DataTypes.TINYINT,
allowNull: false,
defaultValue: 0
}
}, {
sequelize,
tableName: 'user',
timestamps: false,
indexes: [
{
name: "PRIMARY",
unique: true,
using: "BTREE",
fields: [
{ name: "id" },
]
},
{
name: "unique_email",
unique: true,
using: "BTREE",
fields: [
{ name: "email" },
]
},
{
name: "name",
unique: true,
using: "BTREE",
fields: [
{ name: "name" },
]
},
]
});
};
After that I have modified the field "phone" by "telephone" this way:
phone: {
type: DataTypes.STRING(70),
allowNull: false
},
to
telephone: {
type: DataTypes.STRING(70),
allowNull: false
},
Then I have run this:
(env) jgarcia@Javier-PC:/var/www/node_test$ npx sequelize migration:generate --name test_migration
Sequelize CLI [Node: 18.3.0, CLI: 6.4.1, ORM: 6.20.1]
migrations folder at "/var/www/node_test/migrations" already exists. New migration was created at /var/www/node_test/migrations/20220615115322-test_migration.js .
But I get an "empty" migration as you can see here below:
(env) jgarcia@Javier-PC:/var/www/node_test$ cat /var/www/node_test/migrations/20220615115322-test_migration.js
'use strict';
module.exports = {
async up (queryInterface, Sequelize) {
/**
* Add altering commands here.
*
* Example:
* await queryInterface.createTable('users', { id: Sequelize.INTEGER });
*/
},
async down (queryInterface, Sequelize) {
/**
* Add reverting commands here.
*
* Example:
* await queryInterface.dropTable('users');
*/
}
};
I expected that the file 20220615115322-test_migration.js
contained some reference to my change: from phone
to telephone
.
Regards Javier
CodePudding user response:
This is what the migration:generate
CLI command does. It doesn't interact with your models at all. See this SO thread for additional information.
You can pass additional parameters into your migration:generate
command to get the CLI to write some of the code for you. You can also use Sequelize Auto-Migrations, but that package has not seen any updates in a long time and probably has compatibility issues with newer versions of Sequelize.