用 sqlite3 续集不会创建数据库
Posted
技术标签:
【中文标题】用 sqlite3 续集不会创建数据库【英文标题】:Sequelize with sqlite3 doesn't create a database 【发布时间】:2019-01-17 04:14:06 【问题描述】:你能帮我解决这个问题吗? 我将 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 不执行文件。
谢谢。
【问题讨论】:
【参考方案1】:我找到了一个原因:在配置中应该是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
;
【讨论】:
【参考方案2】:试试看:
./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')
;
【讨论】:
以上是关于用 sqlite3 续集不会创建数据库的主要内容,如果未能解决你的问题,请参考以下文章