Reputation: 106
I have successfully integrated Sequelize ORM with Express Js but i am having troble to migrate db in sequelize. Any help?
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'
});
Upvotes: 4
Views: 359
Reputation: 79
Have you loaded your model and synced the database? Your code doesn't really show me the rest of your configuration just the connection string.
I suggest after installing sequelize-cli that your use
$ sequelize help:model:create
It will tell you how to create a model.
You'll have to import the model files, associate methods, sync those models with the database.
// 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);
});
}
}
Upvotes: 0
Reputation: 336
In order to use the CLI you need to install the respective package:
npm install --save sequelize-cli
Further and more detailed information about the available commands can be obtained via the help command:
$ sequelize help:init
$ sequelize help:db:migrate
$ sequelize help:db:migrate:undo
For further info use this link Sequelize
Upvotes: 5