1
votes

First of all, I already searched every single question about this topic here in StackOverflow and in almost everywhere, so I'm asking if you can help me.

I'm making a web app using ExpressJS(v4.15.5) and Sequelize(v4.22.6). I defined the models, migrations, associations and inclusively add an alias to all associations so I'll use this alias when making the eager loading. If I try to eager load a specific association tells me that X is not associated to Y and of course, if I try to eager load all associations using {include: [{all: true}]} it doesn't return any association.

I already verified if the sync was being done and it is, in file bin/www:

models.sequelize.sync().then(function() {
    /**
     * Listen on provided port, on all network interfaces.
     */
    server.listen(port, function() {
        debug('Express server listening on port ' + server.address().port);
    });
    server.on('error', onError);
    server.on('listening', onListening);
});

Above are my models MessageGroup and Message and my routes/index.js file.

MessageGroup Model

'use strict';

module.exports = (sequelize, DataTypes) => {
    var MessageGroup = sequelize.define('MessageGroup', {
        slug: DataTypes.STRING,
        name: DataTypes.STRING
    }, {
        classMethods: {
            associate: function (models) {
                // associations can be defined here
                MessageGroup.hasMany(models.Message, { as:'messages', foreignKey: "messageGroupId" });
            }
        }
    });

    return MessageGroup;
};

Message model

'use strict';

module.exports = (sequelize, DataTypes) => {
    var Message = sequelize.define('Message', {
        slug: DataTypes.STRING,
        name: DataTypes.STRING,
        description: DataTypes.TEXT
    }, {
        classMethods: {
            associate: function (models) {
                // associations can be defined here
                Message.belongsTo(models.MessageGroup, { as:'messageGroup', foreignKey: "messageGroupId" });
                Message.hasMany(models.MessageParameter, { as:'messageParameters', foreignKey: "messageId" });
            }
        }
    });

    return Message;
};

routes/index.js file

<!-- language: javascript -->
const express = require('express');
const router = express.Router();
const models = require('../models');

router.get('/data', function (req, res, next) {
    models.MessageGroup.findAll({include: [{all: true}]}).then(result => {
        res.json({data: result});
    });
});


module.exports = router;

Thanks in advance.

1

1 Answers

0
votes

Problem solved. I was using sequelize v3 syntax. In the v4 syntax models classMethods dissapear and associations are made like this:

 ...   
     Message.associate = function (models) {
            // associations can be defined here
            Message.belongsTo(models.MessageGroup, { as:'messageGroup', foreignKey: "messageGroupId" });
            Message.hasMany(models.MessageParameter, { as:'messageParameters', foreignKey: "messageId" });
        };
    ...

More info here http://docs.sequelizejs.com/manual/tutorial/upgrade-to-v4.html