loopback icon indicating copy to clipboard operation
loopback copied to clipboard

relation method add not working, generating wrong query

Open rahpuser opened this issue 10 years ago • 12 comments

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 ?

rahpuser avatar Sep 21 '16 23:09 rahpuser

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

quote source

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.

rahpuser avatar Sep 21 '16 23:09 rahpuser

Documentation improvement.

rahpuser avatar Sep 21 '16 23:09 rahpuser

@rahpuser patientId is the foreign key property. What's the inconsistency?

crandmck avatar Oct 03 '16 23:10 crandmck

@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.

rahpuser avatar Oct 03 '16 23:10 rahpuser

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.

raymondfeng avatar Oct 11 '16 21:10 raymondfeng

ok @raymondfeng , then it seems to be a bug. Thanks.

rahpuser avatar Oct 12 '16 21:10 rahpuser

@jannyHou FYI

crandmck avatar Oct 14 '16 22:10 crandmck

somebody could find where is the bug? I'm working on loopback 3.x and still having this issue

estebanDarriens avatar Jun 15 '17 23:06 estebanDarriens

need sometime to reproduce the problem. will get back to you asap

jannyHou avatar Jun 16 '17 17:06 jannyHou

@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

rahpuser avatar Jun 16 '17 20:06 rahpuser

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.

stale[bot] avatar Aug 23 '17 14:08 stale[bot]

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.

stale[bot] avatar Sep 06 '17 17:09 stale[bot]