Rastalamm
Rastalamm

Reputation: 1782

Sequelize Model not syncing after Migration

I am able to successfully bulkCreate on a sequelize model.

I ran a migration to add a column called utc_offset to that model which worked.

The issue I am running into is that after I reset my express server, the model in my files does not seem to have the new column when trying to create new instances. The DB is showing its there, but when I pass in a value to that column, it does not add the value for the new column utc_offset.

I believe it has something to do with db.sequelize.sync() - When I set he options to {force: true} it deleted the model and would not re-create it.

Below is my code:

Server:

var express = require('express'),
    app = express(),
    api,
    db = require('./models');

api = app.listen(config.address.port, config.address.host, function () {
    var host = api.address().address,
        port = api.address().port

    console.log('API listening at http://%s:%s', host, port);
    db.sequelize.sync();
});

index.js in my models folder 'use strict';

var fs        = require('fs');
var path      = require('path');
var cls       = require('continuation-local-storage');
var namespace = cls.createNamespace('ss-transaction');
var Sequelize = require('sequelize');
Sequelize.cls = namespace;
var basename  = path.basename(module.filename);
var env       = process.env.NODE_ENV || 'development';
var config    = require('../config/config.js');
var sequelize = new Sequelize(config.db.database, config.db.username, config.db.password, {
    host: config.db.host,
    dialect: config.db.dialect,
    define: {underscored: true}
});
var db        = {};

fs
  .readdirSync(__dirname)
  .filter(function(file) {
    return (file.indexOf('.') !== 0) && (file !== basename);
  })
  .forEach(function(file) {
    if (file.slice(-3) !== '.js') return;
    var model = sequelize['import'](path.join(__dirname, file));
    db[model.name] = model;
  });

Object.keys(db).forEach(function(modelName) {
  if ('associate' in db[modelName]) {
    db[modelName].associate(db);
  }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

'lessons' model:

"use strict";

module.exports = function (sequelize, DataTypes) {

    var Lesson = sequelize.define("Lesson", {
        id: {
            type: DataTypes.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },
        title: DataTypes.STRING,

    }, {
        tableName: "lessons",
    });

    return Lesson;
};

'use strict';

module.exports = {
    up: function (queryInterface, Sequelize) {
        return queryInterface.addColumn(
            'lessons',
            'utc_offset',
            Sequelize.INTEGER
        );
    },

    down: function (queryInterface, Sequelize) {
        return queryInterface.removeColumn(
            'lessons',
            'utc_offset'
        );
    }
};

BulkCreate

var lessons = [{title: "lessonA", utc_offest: 1},{title: "lessonB", utc_offest: 9}]

    Lesson.bulkCreate(lessons)
        .then(function () {
            res.sendStatus(201);
        });

Upvotes: 1

Views: 720

Answers (1)

Jan Aagaard Meier
Jan Aagaard Meier

Reputation: 28788

Migrations will only change the table structure in the DB - you have to manually add the utc_offset column to the model

Upvotes: 4

Related Questions