0

I have an express server and it does:

app.db = SequelizeData config.db
app.db.sequelize.sync({force: false}).complete (err) ->
  console.log err
  console.log 'Initialzed database on:'
  console.log config.db

SequelizeData is:

(function() {
  'use strict';
  var Sequelize, fs, path, _;

  _ = require('lodash');

  fs = require('fs-extra');

  path = require('path');

  Sequelize = require('sequelize');

  module.exports = function(config) {
    var db, files, modelPath, myDb, sequelize;
    sequelize = new Sequelize(config.database, config.username, config.password, {
      dialect: 'postgres',
      host: config.host,
      port: config.port,
      logging: false,
      define: {
        charset: 'utf8',
        collate: 'utf8_general_ci'
      }
    });
    db = {};
    modelPath = __dirname + "/models";
    files = fs.readdirSync(modelPath);
    _.each(files, function(file) {
      var model;
      if ('.coffee' === path.extname(file)) {
        model = sequelize["import"](path.join(modelPath, file));
        return db[model.name] = model;
      }
    });
    Object.keys(db).forEach(function(modelName) {
      if ('associate' in db[modelName]) {
        return db[modelName].associate(db);
      }
    });
    myDb = _.assign(db, {
      sequelize: sequelize,
      Sequelize: Sequelize
    });
    return myDb;
  };

}).call(this);

which basically walks through my models and adds them to sequelize. However, although my database exists, it doesn't create any tables. What am I doing wrong?

Thanks!

Shamoon
  • 41,293
  • 91
  • 306
  • 570

1 Answers1

1

Are you sure that you can actually require *.coffee files? You should make sure that the import is actually working.

You might need to do a Sequelize.sync({force:true}) call to get Sequelize to drop the existing tables and recreate them. You'll need to do that on every schema change, or make the schema changes manually.

You can also set logging: true to see what specific SQL commands are being sent.

ps: This is Node: you don't need to wrap your modules in closures: just put them in their own file and require() them.

srlm
  • 3,186
  • 2
  • 27
  • 40
  • That was actually `coffee script` that was then turned into JS and I guess that's how coffee script handles it? – Shamoon Feb 09 '15 at 19:38
  • Well, you're passing .coffee files to `Sequelize.register`, which probably expects .js files. It looks like you can get around that with something like http://stackoverflow.com/a/4769079/2557842. But you can test this really easily just be observing what the import returns. – srlm Feb 09 '15 at 19:45