node.js - sequelize.js how to associate through a 3rd table? -


i have 3 models, poll, category, , poll_to_category.

poll.server.model.js

module.exports = function(sequelize, datatypes) {      var poll = sequelize.define('poll', {             poll_id:{type:datatypes.integer,primarykey:true}         },         {             associate: function(models){                 poll.hasone(models.caetgory, {through:models.polltocategory)             }         }     );      return poll; }; 

category.server.model.js

module.exports = function(sequelize, datatypes) {      var category = sequelize.define('category', {             category_id:{type:datatypes.integer, primarykey:true}         },         {             associate: function(models){                 category.hasmany(models.poll, {through:models.polltocategory)             }         }     );      return category; }; 

poll_to_category.server.model.js

module.exports = function(sequelize, datatypes) {      var polltocategory = sequelize.define('polltocategory', {             ptc_id:{type:datatypes.integer,primarykey:true},             poll_id:datatypes.integer,             category_id:datatypes.integer         },         {             associate: function(models){                 polltocategory.belongsto(models.poll, {foreignkey:'poll_id'}),                 polltocategory.belongsto(models.category, {foreignkey:'category_id'})             }         }     );      return polltocategory; }; 

when try query

db.poll.findall({include:db.category}).then(function(polls,err){     if (err) return res.status(400).send({message: errorhandler.geterrormessage(err)});     res.jsonp(polls); }); 

i following error:

possibly unhandled error: er_bad_field_error: unknown column 'category.poll_id' in 'field list' 

i tried play belongsto / hasone configuration between 3 either above error, or no errors @ recieve poll object attribute 'category' 'null'. im new sequelize.js, , im not sure on how ask question (terminology-wise).

how can eager load category poll, regard above table structure?

you need give alias third table name. this:

associate: function(models){                 poll.hasone(models.caetgory, {through:models.polltocategory, 'polltocategory')         } 

but correct way let sequelize create third entities. this, need insert in associate alias , set fixed 'through' name, sequelize create you.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -