Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

166
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda