1 回答
TA贡献1936条经验 获得超6个赞
根据上面的评论,我意识到我的文件(由Sequelize生成)没有查找放置在子文件夹中的模型文件。相反,它只查找根文件夹中的模型文件 - 与文件级别相同。index.js
index.js
我也不得不使用包来使文件查找子文件夹。glob
index.js
所以我现在的内容是:index.js
'use strict';
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config')[env];
const db = {};
const glob = require("glob")
let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}
const files = glob.sync(__dirname + "/*/*.js")
files.forEach(file => {
const model = sequelize['import'](file);
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;
为了回答具体问题,不,我不必一直做命令。我可以复制 -粘贴-编辑新模型。
添加回答
举报