Get array of associations

9 views
Skip to first unread message

David Gray

unread,
Aug 27, 2016, 11:00:15 AM8/27/16
to Sequelize
Hi 

I need to know want associations I have set on the model.

This is my model for Buildings


Buildings has multiple Rooms


var Buildings = sequelize.define('buildings', buildingsDefinition,
    {
        timestamps: true,
        underscored: false,
        paranoid: true,
        indexes: [
            { fields: ['accId']}
        ],
        engine: 'innodb',
        classMethods:{
            associate:function(models){
                this.hasMany(models.Rooms, {
                    as: 'Rooms',
                    foreignKey: 'buildingId',
                    onUpdate: 'NO ACTION',
                    onDelete: 'NO ACTION',
                    constraints: false
                })
            }
        }
    }
);


In a route, how do I get an array of the associations for this model?

Desired result, something like:


[
    {'Rooms':
        {
            as: 'Rooms',
            foreignKey: 'buildingId',
            onUpdate: 'NO ACTION',
            onDelete: 'NO ACTION',
            constraints: false
        }
    }
]


Something like Models.Buildings.getAssociations()

Reply all
Reply to author
Forward
0 new messages