使用 sqlite3 进行 Sequelize 不会创建数据库

Sequelize with sqlite3 doesn't create a database

你能帮我解决这个问题吗? 我将 sqlite3 与 sequelize npm 包一起使用。 运行ning 迁移后,我没有在控制台中看到错误,但我也没有看到任何数据库文件。我也可以 运行 一次又一次地迁移,这看起来不像是正确的行为。 这是我的 /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 不会执行文件.

谢谢。

试一试:

./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')
};

我找到了一个原因:在config:

中应该是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
    }
};