Sequelize Code First结合MySQL无法通过命令生成数据库求助
执行
npx sequelize-cli db:migrate失败排查请求 执行npx sequelize-cli db:migrate命令时无法生成数据库表,已确认MySQL安装完成、Workbench可正常连接,且手动创建了car-rental数据库。以下是项目相关代码文件:
package.json
"dependencies": { "express": "^4.19.2", "express-handlebars": "^7.1.3", "mysql2": "^3.10.1", "sequelize": "^6.37.3" }, "devDependencies": { "sequelize-cli": "^6.6.2" }
config\config.json
{ "dev": { "username": "<my_name>", "password": "<my_password>", "database": "car-rental", "host": "127.0.0.1", "dialect": "mysql", "port": 12321 } }
migrations\create-companies.js
'use strict'; /** @type {import('sequelize-cli').Migration} */ module.exports = { async up(queryInterface, Sequelize) { await queryInterface.createTable('Companies', { id: { allowNull: false, autoIncrement: true, primaryKey: true, type: Sequelize.INTEGER }, companyId: { allowNull: false, type: Sequelize.UUIDV4 }, companyName: { type: Sequelize.STRING }, createdAt: { allowNull: false, type: Sequelize.DATE }, updatedAt: { allowNull: false, type: Sequelize.DATE } }); }, async down(queryInterface, Sequelize) { await queryInterface.dropTable('Companies'); } };
modals\index.js
'use strict'; const fs = require('fs'); const path = require('path'); const Sequelize = require('sequelize'); const basename = path.basename(__filename); const config = require(__dirname + '/../config/config.json')['dev']; const db = {}; let sequelize = new Sequelize(config.database, config.username, config.password, { host: config.host, dialect: config.dialect, port: config.port }); fs .readdirSync(__dirname) .filter(file => { return ( file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js' && file.indexOf('.test.js') === -1 ); }) .forEach(file => { const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes); db[model.name] = model; }); Object.keys(db).forEach(modelName => { if (db[modelName].associate) { db[modelName].associate(db); } }); db.sequelize = sequelize; db.Sequelize = Sequelize; module.exports = db;
modals\companies.js
'use strict'; const { Model } = require('sequelize'); module.exports = (sequelize, DataTypes) => { class Companies extends Model { /** * Helper method for defining associations. * This method is not a part of Sequelize lifecycle. * The `models/index` file will call this method automatically. */ static associate(models) { // define association here } } Companies.init({ companyId: { allowNull: false, type: DataTypes.UUIDV4 }, companyName: DataTypes.STRING }, { sequelize, modelName: 'Companies', }); return Companies; };
排查方向
- 配置校验:确认
config/config.json中的username、password、port与MySQL实际配置一致,尤其是端口12321是否为MySQL服务监听端口(默认是3306,修改过需核实) - UUID类型修正:Sequelize中UUID类型应使用
Sequelize.UUID,迁移文件和模型文件里的UUIDV4需改为UUID;同时建议给companyId添加默认值defaultValue: Sequelize.UUIDV4,避免插入数据时因无值报错 - 路径拼写修正:
modals是拼写错误,sequelize-cli默认读取models目录,路径错误会导致模型无法加载,需修改目录名为models并同步代码中的路径引用 - 权限检查:确认MySQL用户
<my_name>拥有car-rental数据库的建表权限,可在Workbench执行GRANT ALL PRIVILEGES ONcar-rental.* TO '<my_name>'@'localhost';后刷新权限重试 - 依赖重新安装:执行
rm -rf node_modules package-lock.json && npm install,确保依赖版本兼容且安装完整 - 查看详细日志:执行
npx sequelize-cli db:migrate --debug,通过详细错误信息定位具体问题
内容的提问来源于stack exchange,提问作者hhpr98
相关产品推荐
相关产品推荐

