Create a database to store module name along with it parent_module_id

create module in python jupyter notebook
modulenotfounderror: no module named
python modules
importerror: attempted relative import with no known parent package
how to create a module in python
python import local module
python import package
python dbm example

Okay i have a table, where i store names of all the modules or programs, somewhat like this

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

Now i want to store parent module of all the modules such as we need to have nodejs installed to start using ionic or for tensorflow we need to have python. Now i had two i included a column named parent_id which was foreign_key referencing id of the same 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         |
+----+----------------+----------+

This helped me give parents the option of having multiple child, like nodejs had two childs angular and ionic.

I am using sequelize and i have setup the association as any regular association.

//--------- 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();

Output is somewhat like this:

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

which was quite expected, but i want to get the parent modules array inside the child entiries, somewhat like this:

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

I know i can change the parent_id column to child id map modules in an opposite way but that excludes the possibility of a parent having multiple child and will show only one child per parent.

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

So how can achieve the desired output and what modification do need to make in the associations or the entire table structure by including a third table which will have two foreing key referring the same table, somewhat like this but this doesn't look so conventional, or to be honest i have never seen stuff like this before so i am not quite sure if this the correct way or not.

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

And hence i am confused how to solve this problem and i also happen to be a newbie and beginner. Thanks in advance.

You can create a related many-to-many table like this:-

CREATE TABLE Language ( lng_id INT NOT NULL, lng_name VARCHAR(40) NOT NULL, Description VARCHAR (100) NOT NULL, Image BLOB NOT NULL, PRIMARY KEY (lng_id)

CREATE TABLE dependency_lng ( lng_id1 INT NOT NULL, // FK related to language.lng_id lng_id2 INT NOT NULL), // FK related to language.lng_id CONSTRAINT pk_dependency_lng PRIMARY KEY (lng_id1, lng_id2)

You could add then as many entries as you want along side any Foreign keys if your requirement demands.

P.S :--- Also if you want, auto incrementing column is not needed. The PRIMARY KEY can be (lng_id1,lng_id2)

Python Modules: Creating, Importing, and Sharing, Modules are units that store code and data, provide code-reuse to Python projects, You can also understand them as packages of names, which when imported is suitable for initializing values, e.g. for making the connection to a database. Stack Overflow Public questions and answers; All nested rows does not inserted in a table. Create a database to store module name along with it parent_module

Hope you need a self referring association.

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

Note that you don't need belongs to association and need to add keyword as for this association.

and your query

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

Hope this would give what you expect. You don't need a auxiliary table for do this.

dbm — Interfaces to Unix “databases”, Open database for reading and writing, creating it if it doesn't exist. 'n'. Always create a new, Module shelve. Persistence module which stores non-string data​. The filename argument is the name of the database file. The optional flag  Module Reference Random Module Python MySQL Create Database Previous Next Creating a Database. To create a database in MySQL, use the "CREATE DATABASE" statement:

Self referring association will worked

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

Note that we need a belongsTo with as association and not a hasMany.

so the with the normal query

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

and it would result in the desired result

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

Import: Modules and Packages, Let's create our own module and import it into an interactive Python session. faces, and a database module that is used to store and update “seen” faces. Python package is a directory containing a file with the name __init__.py , along with  That's it now you have connected to the MySQL database. 2.1. Creating Databases. Now, we will create a database with the name datacamp. To create a database in MySQL, we use CREATE DATABASE database_name statement.

Introduction to Python SQL Libraries – Real Python, The mysql.connector Python SQL module contains a method .connect() that Create a database named sm_app for your social media app in the You'll pass in the connection object that you created in the previous section, along with the All you have to do is store the query in a string variable and then  The way to change the property from the default value is to select the class module in the VBE Project Explorer, select the class module of interest, access the Properties Window (if necessary, use F4 to make it visible), and change the Instancing property to 2-PublicNotCreatable.

Python REST APIs With Flask, Connexion, and SQLAlchemy – Part , You'll also be using SQLAlchemy to create a database as well as interact with it. You're going to use SQLite as the database engine to store the PEOPLE data. A SQL query getting all of the data in our person table, sorted by last name, would Along the way, you'll create two Python modules, config.py and models.​py  Excel database files make it easy to enter, store, and find specific information. Whether it's a personal list of phone numbers, a contact list for an organization, or a collection of coins, Microsoft Excel has built-in tools to keep track of data and find specific information.

Fundamentals: Module | The Definitive Guide to Yii 1.1, Module. Creating Module; Using Module; Nested Module. A module is a The following shows the typical directory structure of a module named forum : to store module parameters, and use CWebModule::components to share application we should use the route parentModuleID/childModuleID/​controllerID/actionID . Creating a Multi Module Project This guide shows you how to create a multi-module project with Spring Boot. The project will have a library jar and a main application that uses the library.

Comments
  • What ever you suggested is already happening as i have mentioned in my question, but that is not the desired result