初级开发人员。
我正在测试我的API。我在MySQL上使用Node v18、Express v4和Sequelize v6。
我的config.js
require('dotenv').config();
const { DB_NAME, DB_USERNAME, DB_PASSWORD, HOST } = process.env;
module.exports = {
development: {
"username": DB_USERNAME,
"password": DB_PASSWORD,
"database": DB_NAME,
"host": HOST,
"dialect": "mysql",
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
},
staging: {
"username": DB_USERNAME,
"password": DB_PASSWORD,
"database": DB_NAME,
"host": HOST,
"dialect": "mysql",
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
},
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql"
}
}
这是我的模型index.js
const { Sequelize, DataTypes } = require('sequelize');
const env = process.env.NODE_ENV || "development";
const config = require('../../../config/config.js')[env];
const sequelize = new Sequelize(
config.database, config.username, config.password, {
host: config.host,
dialect:config.dialect,
operatorsAliases: false,
pool: {
max: config.pool.max,
min: config.pool.min,
acquire: config.pool.acquire,
idle: config.pool.idle
},
port: 3306
}
)
sequelize.authenticate()
.then(() => {
console.log(`Database connected 🥳`)
})
.catch(err => {
console.error(`Error: `, err)
})
const db = {}
db.Sequelize = Sequelize
db.sequelize = sequelize
db.abouts = require('./abouts')(sequelize, DataTypes);
db.clients = require('./clients')(sequelize, DataTypes);
db.address = require('./address')(sequelize, DataTypes);
db.career = require('./career')(sequelize, DataTypes);
module.exports = db
它在开发中运行良好。但是,在暂存时,它会抛出“No database selected”Table schema is null
我已经检查了所有的文档和解决方案,没有一个有效。迁移和播种工作除此之外。.env
上的变量命名正确,没有拼写错误。
1条答案
按热度按时间bogh5gae1#
谢谢你的回复。问题不在ORM上,而是在数据库上。我们通过用另一个数据库连接重新部署它来解决这个问题。
谢谢你的回应