我已经成功地将 sequelize ORM与Express Js集成在一起,但我可以在sequelize中迁移db。有什么帮助吗?
var express = require('express');
var Sequelize = require('sequelize');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
var router = express.Router();
var sequelize = new Sequelize('tousif', 'root', 'root', {
host: 'localhost',
dialect: 'mysql'
});
发布于 2016-04-04 04:32:40
为了使用CLI,您需要安装相应的包:
npm install --save sequelize-cli
有关可用命令的更多更详细的信息可以通过help命令获得:
$ sequelize help:init
$ sequelize help:db:migrate
$ sequelize help:db:migrate:undo
有关更多信息,请使用此链接后遗症
发布于 2016-07-19 04:57:01
您是否加载了模型并同步了数据库?您的代码并没有真正显示您配置的其余部分,只显示连接字符串。
我建议安装后缀-cli后,您可以使用
$ sequelize help:model:create
它将告诉您如何创建模型。
您必须导入模型文件、关联方法、将这些模型与数据库同步。
// Expose the connection function
db.connect = function(database, username, password, options) {
if (typeof db.logger === 'function')
console.log("Connecting to: " + database + " as: " + username);
// Instantiate a new sequelize instance
var sequelize = new db.Sequelize(database, username, password, options);
db.discover.forEach(function(location) {
var model = sequelize["import"](location);
if (model)
db.models[model.name] = model;
});
// Execute the associate methods for each Model
Object.keys(db.models).forEach(function(modelName) {
if (db.models[modelName].options.hasOwnProperty('associate')) {
db.models[modelName].options.associate(db.models);
winston.info("Associating Model: " + modelName);
}
});
if (config.db.sync) {
// Synchronizing any model changes with database.
sequelize.sync(
//{ force: true } // use to drop before create
).then(function() {
console.log("Database synchronized");
}).catch(function(err) {
console.log(err);
});
}
}
https://stackoverflow.com/questions/36402704
复制