简体   繁体   中英

MySQL many-to-many relationship in LoopBack

I'm having trouble getting LoopBack to perform a many-to-many join query. Considering the hasManyThrough example from the documentation :

var Physician = ds.createModel('Physician', {name: String});
var Patient = ds.createModel('Patient', {name: String});

var Appointment = ds.createModel('Appointment', {
    physicianId: Number,
    patientId: Number,
    appointmentDate: Date
});

Appointment.belongsTo(Patient);
Appointment.belongsTo(Physician);

Physician.hasMany(Patient, {through: Appointment});
Patient.hasMany(Physician, {through: Appointment});

If I try to do a single search to find Patients associated with a particular doctor who have a zip code of 10012, I could try:

physician.patients({where: {zip: 10012}}, fn);

However, the search on the physician's patients is actually only searching on the Appointments table. Is there any way to do a simple search that will be performed on the specific physician's patients directly?

LoopBack implements the hasMany/through relation for physician.patients() as follows:

Appointment.find ({ where: { physicianId: 1 },
  include: 'patient',
  collect: 'patient' }, callback);

We're considering to support the filter for the 'include' which brings int 'patient' information.

I suggest you open an issue at https://github.com/strongloop/loopback-datasource-juggler/issues .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM