简体   繁体   English

在树中排序联接数据

[英]Sequelize join data in tree

I have 3 models that work like a tree: Plants, Genre and family. 我有3个像树一样工作的模型:植物,流派和家庭。

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. 与类型相同,每1个可以有很多植物,而1个植物可以有1个类型。

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. 现在,我进行查询,以获取与工厂有关的所有数据(体裁和家庭),因此我通过req.params.id在路由中传递了工厂的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. 之后,我尝试做一个include,这样我就可以通过急切的加载来获取数据,因为我需要获取一个与所有与该植物相关的数据的json。

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-> 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
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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