Hello, I have the following relation
'entity1' hasMany Through 'entity1_entity2'
'entity2' hasManyThrough 'entity1_entity2
entity1Object.entity2.add(entity2Object);
In debug mode I can see the following query:
SQL: SELECT `entity1_id`,`entity2_id`,`id` FROM `entity1_entity2` WHERE `entity1_id`=? AND `entity2_id`? ORDER BY `id` LIMIT 1, params: [1,2]
Mysql error:
{ [Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '2 ORDER BY `id` LIMIT 1' ...
Seems like an '=' is missing.. is this a bug ?
Seems like I misunderstood the documentation:
Appointment.belongsTo(Patient);
Appointment.belongsTo(Physician);Physician.hasMany(Patient, {through: Appointment});
Patient.hasMany(Physician, {through: Appointment});
// Now the Physician model has a virtual property called patients:
Physician.patients(filter, callback); // Find patients for the physician
Physician.patients.build(data); // Build a new patient
Physician.patients.create(data, callback); // Create a new patient for the physician
Physician.patients.destroyAll(callback); // Remove all patients for the physician
Physician.patients.add(patient, callback); // Add an patient to the physician
Physician.patients.remove(patient, callback); // Remove an patient from the physician
Physician.patients.findById(patientId, callback); // Find an patient by id
the variable patient and patientId drives me to think that patient was an object, and patientId an id, so in order to get it working I'm doing:
entity1Object.entity2.add(entity2Object.id);
Consistent usage of patientId in the documentation would help clarify add method behavior.
Documentation improvement.
@rahpuser patientId is the foreign key property. What's the inconsistency?
@crandmck look at these lines:
Physician.patients.add(patient, callback); // Add an patient to the physician
Physician.patients.findById(patientId, callback); // Find an patient by id
One is using patient and the other one is using patientId.
But both methods accept an id, so that different drives developers to think add method behave different than findById and accepts a patient instance instead of a patient id.
I haven't used remove so I'm not sure if remove is accepting a patient object or a patient id.
The first argument of add can be either an instance or just id value of the target model. We allow both. If the generated SQL has invalid syntax, it's a bug.
ok @raymondfeng , then it seems to be a bug. Thanks.
@jannyHou FYI
somebody could find where is the bug? I'm working on loopback 3.x and still having this issue
need sometime to reproduce the problem. will get back to you asap
@estebanDarriens not sure wheres the bug, but if you need to get this working, you can pass the id to the function, using the id is working, the issue raises when passing the object
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.