javascript - Sequelize 树中的连接数据

标签 javascript sql node.js postgresql sequelize.js

我有 3 个像树一样工作的模型:植物、流派和家庭。

每个家族可以有很多流派,每个流派与 1 个家族相关联。

类型相同,每个 1 可以有很多植物,1 植物可以有 1 种类型。

所以基于此,我有这个模型:

植物

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

流派
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' });
      }
    }
  );

家庭
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' });
      }
    }
  );

现在,我做了一个查询,我想获取与植物(流派和家族)相关的所有数据,所以我在路由中通过 req.params.id 传递植物的 id。

之后,我尝试进行包含,以便我可以通过急切加载获取数据,因为我需要获取包含与工厂相关的所有数据的 json。

但是我无法获得与其他模型相关的任何数据,仅使用特定的植物表,有什么帮助吗?

这是服务器上的 Controller 代码:
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 }); // 
        })
    }

最佳答案

首先,定义允许您获取数据 Plant->Genre->Family 的关联

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

然后你可以查询
    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
    });

关于javascript - Sequelize 树中的连接数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44303078/

相关文章:

javascript - 黑客可以访问/复制/下载部署在 Node Js 上的 JS 服务器端代码吗?

javascript - 如何选择 d3 中具有特定属性值的元素

javascript - 使用 JavaScript 访问 DLL

javascript - 在单个语句中声明具有额外属性的 javascript 函数?

Javascript,自动选择下拉菜单

mysql - INSERT INTO SELECT 不起作用

javascript - 每个连接请求 Node JS 和 SQL?

javascript - 我如何通过流传递 'context' ?

mysql - UNION 在 ORDER BY 和 LIMIT 之后

android - socket.io xhr 在连接缓慢时出现错误(3G 移动网络)