简体   繁体   中英

Sequelize join data in tree

I have 3 models that work like a tree: Plants, Genre and family.

Each family can have a lot of genres each genre is associated to 1 family.

Same for Genre, each 1 can have a lot of plants and 1 plant can have 1 genre.

So based on that, i have this models:

Plant

"use strict";
var sequelize = require('./index');
var bcrypt = require('bcrypt-nodejs');
var User = require('./User');


module.exports = function (sequelize, DataTypes) {
  var Plant = sequelize.define("Plant", {
    specie: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    description: {
      type: DataTypes.TEXT,
      allowNull: true,
      defaultValue: "No description for this plant yet"
    },
    directory: {
      type: DataTypes.STRING,
      allowNull: false
    },
     genreId: {
      type: DataTypes.INTEGER,
      allowNull: true
    }
  },
    {
      associate: function (models) {
        Plant.hasMany(models.Foto, { foreignKey: "plantId", as: 'fotos' });
      }
    }
  );

Genre

module.exports = function (sequelize, DataTypes) {
  var Genre = sequelize.define("Genre", {
    name: {
      type: DataTypes.STRING,
      allowNull: false
    },
    familyId: {
      type: DataTypes.INTEGER,
      allowNull: true
    },
    directory: {
      type: DataTypes.STRING,
      allowNull: false
    }
  },
    {
      associate: function (models) {
        Genre.hasMany(models.Plant, { foreignKey: "genreId", as: 'plant' });
      }
    }
  );

Family

module.exports = function (sequelize, DataTypes) {
  var Family = sequelize.define("Family", {
    name: {
      type: DataTypes.STRING,
      allowNull: false
    },
      directory: {
        type: DataTypes.STRING,
        allowNull: false
      }
  },
    {
      associate: function (models) {
        Family.hasMany(models.Genre, { foreignKey: "familyId", as: 'genre' });
      }
    }
  );

now, i do a query where i want to get all data related to the plant(genre and family) so i pass the id for the plant in the routing, via req.params.id.

after that i try to do a include so i can get the data with eager loading, because i need to get a json with all the data related to the plant.

But i can't get any data related to the other models, just with the specific plant table, any help?

Here is the controller code on the server:

specificPlant: function (req, res, next) {
        Plant.findAll({
            where: {
                id: req.params.id,
            },
            include: [{ all: true }] 

        }).then(function (plant) {
            console.log(plant);
            return res.send(plant);
        }).catch(function (err) {
            return res.status(400).send({ message: err.stack }); // 
        })
    }

First, define associations that will allow you to get data Plant->Genre->Family

Plant.hasMany(models.Genre, {foreignKey: "genreId", as: 'genre' });
Genre.hasMany(models.Family, { foreignKey: "familyId", as: 'family' });

Then you can query

    Plant.findAll({
        where: {
            id: req.params.id,
        },
        include: [{
            model: Genre,
            as: 'genre',
            include: [{
                model: Family,
                as: 'family'
            }]
        }]
    }).then(function (plant) {
        //plant
        //plant.genre
        //plant.genre.family
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM