sql - 创建一个数据库来存储模块名称及其 parent_module_id

标签 sql database database-design foreign-keys sequelize.js

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

+----+----------------+
|id  |module          |
+----+----------------+
|1   |node js         |
+----+----------------+
|2   |python          |
+----+----------------+
|3   |angular         |
+----+----------------+
|4   |ionic           |
+----+----------------+
|5   |tensorflow      |
+----+----------------+
|6   |jupyter notebook|
+----+----------------+

现在我想存储所有模块的父模块,例如我们需要 nodejs安装开始使用 ionictensorflow我们需要有python .现在我有两个我包含一个名为 parent_id 的列这是foreign_key引用 id同一张 table 的。
+----+----------------+----------+
|id  |module          |parent_id |
+----+----------------+----------+
|1   |node js         |null      |
+----+----------------+----------+
|2   |python          |null      |
+----+----------------+----------+
|3   |angular         |1         |
+----+----------------+----------+
|4   |ionic           |1         |
+----+----------------+----------+
|5   |tensorflow      |2         |
+----+----------------+----------+
|6   |jupyter notebook|2         |
+----+----------------+----------+

这帮助我为 parent 提供了拥有多个 child 的选择,比如 nodejs 有两个 angular 和 ionic 的 child 。

我正在使用 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'},
  ]}]

我知道我可以更改 parent_id列至 child id以相反的方式映射模块,但这排除了 parent 有多个 child 的可能性,并且每个 parent 只显示一个 child 。
+----+----------------+----------+
|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|
+----+----------------+

因此我很困惑如何解决这个问题,而且我也恰好是新手和初学者。提前致谢。

最佳答案

您可以像这样创建一个相关的多对多表:-

创建表语言(
lng_id INT NOT NULL,
lng_name VARCHAR(40) 非空,
说明 VARCHAR (100) NOT NULL,
图像 BLOB 不为空,
主键 (lng_id)

创建表依赖_lng (
lng_id1 INT NOT NULL,//与语言相关的 FK.lng_id
lng_id2 INT NOT NULL),//与语言相关的 FK.lng_id
约束 pk_dependency_lng 主键 (lng_id1, lng_id2)

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

P.S :--- 另外,如果您愿意,不需要自动递增列。 PRIMARY KEY 可以是 (lng_id1,lng_id2)

关于sql - 创建一个数据库来存储模块名称及其 parent_module_id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54083656/

相关文章:

sql - 插入参数值和 JSON 字符串值

使用变量在存在索引的情况下进行 MySQL 排名

sql - 跟踪 Rails 3 SQL 查询

php - 我们可以在同一页面的另一种表单中使用来自一种表单的用户输入数据吗

Mysql:将带有数字和字母的一列拆分为两列

SQL ON DELETE CASCADE,删除是通过哪种方式发生的?

sql - 索引跳过扫描仿真以检索不同的产品 ID 和附加列的最小值/最大值

database - 打开 adt、adi 和 adm 文件

django - 在 Postgres 中表示政策人口数据的最佳方式

ruby-on-rails - Rails 用户 has_one :profile vs. 显示页面