能帮我解决问题吗? 我将sqlite3与sequelize npm包一起使用。运行迁移后,我在控制台中看不到错误,但也看不到任何数据库文件。另外,我可以一次又一次地执行迁移,这看起来不像是正确的行为。 这是我的 /config/config.js 文件:
const path = require('path');
module.exports = {
test: {
username: 'root',
password: 'root',
database: path.join(__dirname, '..', 'database_test.sqlite'),
host: 'localhost',
dialect: 'sqlite',
logging: console.log,
operatorsAliases: false
}
};
这是 migrations / XXXXXXXXXXXXXX-create-appeal.js 文件:
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('appeals', {
appealId: {
allowNull: false,
primaryKey: true,
type: Sequelize.UUID
},
name: {
allowNull: false,
type: Sequelize.STRING
},
description: {
type: Sequelize.STRING(511)
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
}),
down: queryInterface => queryInterface.dropTable('appeals')
};
这是 models / index.js 文件
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(path.join(__dirname, '..', 'config', 'config.js'))[env];
const db = {};
let sequelize = new Sequelize(config.database, config.username, config.password, config);
fs
.readdirSync(__dirname)
.filter(file => (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js'))
.forEach(file => {
const model = sequelize.import(path.join(__dirname, 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;
这是 models / appeal.js 文件:
'use strict';
module.exports = (sequelize, DataTypes) => {
const Appeal = sequelize.define('appeals', {
appealId: {
allowNull: false,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
type: DataTypes.UUID,
validate: {
isUUID: 4
},
get() {
return this.getDataValue('appealId').toLowerCase();
}
},
name: {
type: DataTypes.STRING,
allowNull: false
},
description: {
type: DataTypes.STRING(511)
}
}, {
tableName: 'appeals'
});
Appeal.associate = models => {
// associations can be defined here
};
return Appeal;
};
另一个奇怪的事情是:如果将控制台日志或错误抛出到 models / index.js 中,我什么也看不到,因此nodejs不会执行文件。
谢谢。
答案 0 :(得分:2)
我发现了一个原因:它应该是storage
而不是配置中的database
:
const path = require('path');
module.exports = {
test: {
username: 'root',
password: 'root',
storage: path.join(__dirname, '..', 'database_test.sqlite'),
host: 'localhost',
dialect: 'sqlite',
logging: console.log
}
};
答案 1 :(得分:0)
尝试一下:
./ config.js
const path = require('path');
module.exports = {
development: {
username: 'root',
password: 'root',
database: path.join(__dirname, '..', 'database_test.sqlite'),
host: 'localhost',
dialect: 'sqlite',
logging: true,
operatorsAliases: false
}
};
.sequelizerc
const path = require('path');
module.exports = {
'models-path': path.resolve('./models'),
'seeders-path': path.resolve('./seeders'),
'migrations-path': path.resolve('./migrations'),
'config': path.resolve('./config', 'config.js')
};