代码之家  ›  专栏  ›  技术社区  ›  Satnam Sandhu

创建数据库以存储模块名称及其父模块ID

  •  0
  • Satnam Sandhu  · 技术社区  · 6 年前

    好的,我有一个表,我在其中存储所有模块或程序的名称,有点像这样

    +----+----------------+
    |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|
    +----+----------------+
    

    因此,我很困惑如何解决这个问题,我也恰好是一个新手和初学者。事先谢谢。

    3 回复  |  直到 6 年前
        1
  •  2
  •   kamta nath    6 年前

    您可以创建一个相关的多对多表,如下所示:

    创建表语言( 液化天然气输入不为空, 液化天然气名称varchar(40)不为空, 说明varchar(100)不为空, 图像blob不为空, 主键(Lng_ID)

    创建表依赖项( lng_id1 int不为空,//fk与language.lng_id相关 lng_id2 int不为空),//fk与language.lng_id相关 约束pk_依赖项_lng主键(lng_id1,lng_id2)

    如果需要的话,您可以在任何外键旁边添加任意数量的条目。

    另外,如果你愿意,也不需要自动递增列。主关键字可以是(液化天然气储罐id1、液化天然气储罐id2)

        2
  •  0
  •   margherita pizza    6 年前

    希望你需要一个自我介绍协会。

    module.associate = function(models) {
        Module.hasMany(Module, {
          foreignKey: 'parent_id',
          as:'sub_entry'
        })
      };
    

    注意 你不需要 属于关联,需要添加关键字 as 对于这个协会。

    你的询问

    let modules = await models.Module.findAll({
        include: [{
          model: models.Module,
          as:'sub_entry'
        }]
    

    希望这能满足你的期望。这样做不需要一个辅助桌。

        3
  •  0
  •   Satnam Sandhu    6 年前

    自我介绍协会会有效的

    module.associate = function(models) {
      Module.belongsTo(Module, {
        foreignKey: 'parent_id',
        as:'parent'
      })
    };
    

    注意 我们需要 belongsTo 具有 as 协会而不是 hasMany .

    所以,使用普通查询

    let modules = await models.Module.findAll({
      include: [{
        model: models.Module,
        as:'parent'
      }]
    });
    

    它会产生期望的结果

    [{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'},
      ]}]