sequelize.js - How to set params in join table via setAssociation options in sequelize? -


i've defined schema of table name labels_association, associated labels , model x follows:

'use strict';  module.exports = {   up: function(queryinterface, sequelize) {     return queryinterface.createtable('labels_association', {       labelid: {         allownull: false,         type: sequelize.integer       },       associationid: {         allownull: false,         type: sequelize.integer       },       associationtype: {         allownull: false,         type: sequelize.enum('x', 'y')       }     });   },   down: function(queryinterface) {     return queryinterface.droptable('labels_association');   } }; 

defining has , belongs many relationship of model x labels via labels_association table

x.belongstomany(label, {   as: 'labels',   through: {     model: 'labels_association',     attributes: ['associationid', 'associationtype'],     scope: {       associationtype: 'x'     }   },   foreignkey: 'associationid',   otherkey: 'labelid' }); 

though @ time of setting labels instance of model x, not able set associationtype 'x', while should've been taken care of scope attribute in belongstomany association.

instanceofx.setlabels(labelids, {   associationtype: 'x' }) 

how can set associationtype value 'x' in above scenario?

in reference issue tracked on github https://github.com/sequelize/sequelize/issues/5378

needed create model relationship table follows:

let labelassociation = sequelize.define('labelassociation', {   associationtype: {     type: string     // etc   } }, {   tablename: 'label_associations' });  x.belongstomany(label, {   as: 'labels',   through: {     model: labelassociation,     attributes: ['associationid', 'associationtype'],     scope: {       associationtype: 'x'     }   },   foreignkey: 'associationid',   otherkey: 'labelid' }); 

explanation follows:

the string argument(through.model in belongstomany options) define table name autogenerated model. if haven't defined model join model, need yes, otherwise sequelize doesnt know these attributes exist.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -