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

164
Views
cómo conectar tres tablas a la identificación de una tabla en secuela

Estoy creando una aplicación usando nodejs, express y sequelize. en una API, quiero obtener datos de 4 tablas diferentes con un pid como entrada. Estoy tratando de asociarme. Estoy siguiendo un flujo de mvc. como tabla 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' }); };

y hay una mesa 4 con la misma estructura que T3. Antes de usar asociaciones, usé la referencia como en el código para configurar la clave externa y la conecté manualmente en phpmyadmin. ahora me confundí con lo que hacer. Quiero pasar p_id de la tabla 1 en mi req.body y obtener datos de las tablas de descanso. cualquier entrada es apreciada. Saludos.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

En el archivo donde desea obtener sus datos, importe su modelo de PatientModel y los demás. Y podrías hacer lo siguiente.

 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 })

Encuentre el tiempo para leer más en los documentos de secuela aquí 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!