Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
how to connect three tables to one table's id in sequelize

i'm building an app using nodejs,express and sequelize. in an api i want to get data from 4 different table with one pid as input. i'm trying to associate . i'm following an mvc flow. how to table 1

/* jshint indent: 2 */

// const machine_data = require("./machine_data");

module.exports = function(sequelize, DataTypes) {
    return sequelize.define('patient_data', {
      sl_no: {
        type: DataTypes.INTEGER(11),
        autoIncrement: true,
        allowNull: false,
        primaryKey: true,
      
      },
      patient_name: {
        type: DataTypes.STRING(35),
        allowNull: false
      },
      p_id: {
        type: DataTypes.STRING(100),
        allowNull: false,
      
      
    
     
    },
    
    {
      associate:function(modals){
        patient_data.hasMany(modals.master_session,{ foreignKey: 'p_id' });
        patient_data.hasMany(modals.graph_data,{ foreignKey: 'pid' });
        patient_data.hasMany(modals.vital_data,{ foreignKey: 'master_id' })
      },
    },
    
    {
     sequelize,
     tableName: 'patient_data'
   });
 };

Table 2
module.exports = function(sequelize, DataTypes) {
    return sequelize.define('master_session', {
      master_id: {
        type: DataTypes.INTEGER(11),
        autoIncrement: true,
        allowNull: false,
        primaryKey: true,
        
  
      },
      session_name: {
        type: DataTypes.STRING(35),
        allowNull: true
      },
 {
      associate:function(modals){
        master_session.belongsTo(modals.patient_data,{ foreignKey: 'p_id' });
       
      },
    },


    {
     sequelize,
     tableName: 'master_session'
   });
 };

table 3
module.exports = function(sequelize, DataTypes) {
    return sequelize.define('graph_data', {
      sl_no: {
        type: DataTypes.INTEGER(11),
        autoIncrement: true,
        allowNull: false,
        primaryKey: true,
        foreignKey:true,
        refereces:{
          model:'patient_data',
          key:'p_name'
        }
        
       
  
      },
      document: {
        type: DataTypes.STRING(2000),
        allowNull: true
      },

      pid: {
        type: DataTypes.STRING(200),
        allowNull: true
      },

      session: {
        type: DataTypes.STRING(200),
        allowNull: true
      },
          
    },
    {
      associate:function(modals){
        graph_data.hasMany(modals.patient_data,{ foreignKey: 'pid' });
      
      },
    },
    
    {
     sequelize,
     tableName: 'graph_data'
   });
 };

and there's a table 4 with same structure as T3 . Before using associations i used reference as in the code to set foreign key and manually connected this in phpmyadmin. now i got confused with what to do. I want to pass p_id from table 1 in my req.body and fetch data from rest tables. any input is appreciated. regards.

about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

In the file where you want to fetch your data, import your PatientModel and the others. And you could do the following.

patientModel.findAll({
 include: [
  {
   model: masterSessionModel,
   attributes: // attributes from master_session
  },
  {
   model: graphDataModel,
   attributes: // attributes from graphData
  },
  // other associated models
 ]
}).then(myData => { // do something with it })

Find the time to read more in the sequelize docs over here https://sequelize.org/master/manual/assocs.html#eager-loading-example ๐Ÿ‘ˆ

about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!