这是我的index.js和config.json文件:
const { Sequelize } = require('sequelize');
const env = process.env.NODE_ENV || 'development';
const config = require('../config/config.json')[env];
const sequelize = new Sequelize({
dialect: config.dialect,
storage: config.storage,
dialectModule: require('mysql2')
});
sequelize.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch(err => {
console.error('Unable to connect to the database:', err);
});
module.exports = sequelize;
config.json:
{
"development": {
"dialect": "sqlite",
"storage": "./database.sqlite3",
"migrations-path": "./migrations"
},
"test": {
"dialect": "sqlite",
"storage": "./database_test.sqlite3",
"migrations-path": "./migrations"
},
"production": {
"dialect": "sqlite",
"storage": "./database.sqlite3",
"migrations-path": "./migrations"
}
}
运行后出错
npx sequelize-cli db:migrate
:
已加载配置文件“config/config.json”。
使用环境“开发”。错误:请手动安装 mysql2 软件包
我尝试全局安装 mysql2 并使用此命令:
npx sequelize-cli db:migrate --env development
,但没有任何效果
mysql2 应该安装在您的项目中:
npm install mysql2 --save
那么,
dialect: 'mysql',
更改此行并再次运行迁移命令。
npx sequelize-cli db:migrate --env development