好的,我有一个表,我在其中存储所有模块或程序的名称,有点像这样
  
  +----+----------------+
|id  |module          |
+----+----------------+
|1   |node js         |
+----+----------------+
|2   |python          |
+----+----------------+
|3   |angular         |
+----+----------------+
|4   |ionic           |
+----+----------------+
|5   |tensorflow      |
+----+----------------+
|6   |jupyter notebook|
+----+----------------+
  
   现在我想存储所有模块的父模块,如我们需要的
   
    nodejs
   
   安装以开始使用
   
    ionic
   
   或为了
   
    tensorflow
   
   我们需要拥有
   
    python
   
   . 现在我有两个,我包括一个叫
   
    parent_id
   
   那是
   
    foreign_key
   
   引用
   
    id
   
   在同一张桌子上。
  
  +----+----------------+----------+
|id  |module          |parent_id |
+----+----------------+----------+
|1   |node js         |null      |
+----+----------------+----------+
|2   |python          |null      |
+----+----------------+----------+
|3   |angular         |1         |
+----+----------------+----------+
|4   |ionic           |1         |
+----+----------------+----------+
|5   |tensorflow      |2         |
+----+----------------+----------+
|6   |jupyter notebook|2         |
+----+----------------+----------+
  
   这帮助我给了父母多个孩子的选择,就像诺德兹有两个孩子,棱角分明的和离子型的。
  
  
   我正在使用Sequelize,并且已经将该关联设置为任何常规关联。
  
  //--------- module.model.js------------//
'use strict';
module.exports = (sequelize, DataTypes) => {
  const Module = sequelize.define('Module', {
  }, {
    timestamps: false
  });
  module.associate = function(models) {
    Module.hasMany(Module, {
      foreignKey: 'parent_id',
    });
    Module.belongsTo(Module, {
      foreignKey: 'parent_id',
    });
  };
  return Module;
};
//--------------- index.js-------------//
const models = require('./models');  //module.models.js
async function start() {
  let modules = await models.Module.findAll({
    include: [{
      model: models.Module
    }]
  });
  console.log(modules);
}
start();
  
   输出有点像这样:
  
  [{id: 1, module: 'node js', Module: [
    {id: 3, module: 'angular'},
    {id: 4, module: 'ionic'}
  ]},
 {id: 2, module: 'python', Module: [
    {id: 5, module: 'tensorflow'},
    {id: 6, module: 'jupyter notebook'}
  ]},
 {id: 3, module: 'angular', Module: []},
 {id: 4, module: 'ionic', Module: []},
 {id: 5, module: 'tensorflow', Module: []},
 {id: 6, module: 'jupyter notebook', Module: []}]
  
   这是意料之中的,但我想在子实体中获取父模块数组,有点像这样:
  
  [{id: 1, module: 'node js', Module: []},
 {id: 2, module: 'python', Module: []},
 {id: 3, module: 'angular', Module: [
    {id: 1, module: 'node js'}
  ]},
 {id: 4, module: 'ionic', Module: [
    {id: 1, module: 'node js'},
  ]},
 {id: 5, module: 'tensorflow', Module: [
    {id: 2, module: 'python'},
  ]},
 {id: 6, module: 'jupyter notebook', Module: [
    {id: 2, module: 'python'},
  ]}]
  
   我知道我可以改变
   
    亲本
   
   列到
   
    child id
   
   以相反的方式映射模块,但不包括父级具有多个子级的可能性,并且每个父级只显示一个子级。
  
  +----+----------------+----------+
|id  |module          |child_id  |
+----+----------------+----------+
|1   |node js         |3         |
+----+----------------+----------+
|2   |python          |5         |
+----+----------------+----------+
|3   |angular         |null      |
+----+----------------+----------+
|4   |ionic           |null      |
+----+----------------+----------+
|5   |tensorflow      |null      |
+----+----------------+----------+
|6   |jupyter notebook|null      |
+----+----------------+----------+
  
   那么,如何实现所需的输出以及需要在关联或整个表结构中进行哪些修改,方法是包括第三个表,该表将有两个前导键引用同一个表,有点像这样,但这看起来并不那么传统,或者说实话,我以前从未见过这样的东西,所以我不太确定。如果这个方法正确与否。
  
    +-------------------------------------------------
  |                                     |          |
+----+----------------+        +----+---------+----------+
|id  |module          |        |id  |child_id |parent_id |
+----+----------------+        +----+---------+----------+
|1   |node js         |        |1   | 3       | 1        |
+----+----------------+        +----+---------+----------+
|2   |python          |        |2   | 4       | 1        |
+----+----------------+        +----+---------+----------+
|3   |angular         |        |3   | 5       | 2        |
+----+----------------+        +----+---------+----------+
|4   |ionic           |        |4   | 6       | 2        |
+----+----------------+        +----+---------+----------+
|5   |tensorflow      |
+----+----------------+
|6   |jupyter notebook|
+----+----------------+
  
   因此,我很困惑如何解决这个问题,我也恰好是一个新手和初学者。事先谢谢。