javascript - Sequelize - How to automatically retrieve child records from a query to the parent table? -
i'm using sequelize 3.24.3 connect mysql database.
in database have one-to-many relationship between 2 tables - paper & paperversion. 1 paper can have multiple versions. requirement write single query retrieve paper paper table, returned resultset should contain versions associated paper.
this how models like:
var paper = sequelize.define("paper", { author: datatypes.string, status: datatypes.enum('pending submission','submitted) }); var paperversion = sequelize.define("paperversion", { id: { type: datatypes.integer, primarykey: true, autoincrement: true }, title: datatypes.string, description: datatypes.string }, { classmethods: { associate: function(models){ paperversion.belongsto(models.paper); } } });
this how execute select statements:
papermodel.findall({ }) .then(function(papers){ // processing }); paperversion.find({ where:{ paperid:paper.id, } }).then(function(version){ // processing });
when execute find or findall on either paper or paperversion model records paper or paperversion table. read somewhere possible include records associated tables in resultset. can please let me know how can achieve this.
something this:
papermodel .findall({ include: [{ model: paperversion}] }) .then(function(papers) { // processing });
Comments
Post a Comment