Repository navigation
Description
Outline
When creating many-many relations between models for a mongodb data source, entries are stored in the relation table as per this example:
{
"_id" : ObjectId("537b7f5eb723553355816de6"),
"partId" : "5379f3444d596bea29987bef",
"jobId" : "5379e59d6fd568ba272a940c"
}The job record in referred to in this record is:
{
"_id" : ObjectId("5379e59d6fd568ba272a940c"),
"title" : "test",
...
}When querying the relation, a query such as job.parts() will return an empty array because the job._id property with a value of ObjectId("5379e59d6fd568ba272a940c") will not match the jobId property with a value of "5379e59d6fd568ba272a940c".
The first example (the relation object) is always created this way when using either of the following methods:
job.parts.add(part, cb);or
var PartJob = Part.relations.job.modelThrough;
PartJob.create({
partId: "5379f3444d596bea29987bef",
jobId: "5379e59d6fd568ba272a940c"
}, cb);Workaround
As it stands, the only way found to make a working many-many relation is to use the following method:
var ObjectID = require('mongodb').ObjectID;
var PartJob = Part.relations.job.modelThrough;
PartJob.create({
partId: ObjectID("5379f3444d596bea29987bef"),
jobId: ObjectID("5379e59d6fd568ba272a940c")
}, cb);This creates the relation object:
{
"_id" : ObjectId("537b7f5eb723553355816de6"),
"partId" : ObjectId("5379f3444d596bea29987bef"),
"jobId" : ObjectId("5379e59d6fd568ba272a940c")
}Proposed Solution
Whilst this creates a usable many-many relation, it creates an extra step in the process and invalidates the job.parts.add method, so the casting of the the string ID to an ObjectID should probably occur behind the scenes. This would also avoid having to change the existing public API.
Also (and this should probably have it's own github issue), if you wanted to create a many-many relation over a REST API, it's more efficient to simply pass the IDs of the object you wish to connect and create the relation object directly onto the relation table.
This (as Raymond has pointed out) is a rather hacky approach and in terms of providing a clean fix for the above issue becomes unfeasible as you would still be forced to manually cast the ID properties.
I propose a static method for each relationship on each model class for creating a relation that simply takes the 2 IDs to create a relation for. With this approach, the static method could perform the ID casting without the developer having to concern themselves with it.
For example:
var ObjectID = require('mongodb').ObjectID;
var PartJob = Job.relations.part.modelThrough;
Job.attachPart = function(jobId, partId, callback){
PartJob.create({
partId: ObjectID(partId),
jobId: ObjectID(jobId)
}, function(err, result){
if (err) {
callback(err);
} else {
callback(null, result);
}
});
};
Activity
innerdaze commentedon May 28, 2014
I have tested and can confirm that this issue also exists for the create method on a hasAndBelongsTo relationship
bajtos commentedon Oct 22, 2014
@lsdriscoll can you reproduce the problem using the latest version of loopback-connector-mongodb and loopback-datasource-juggler?
Madsn commentedon Nov 8, 2014
I'm having this issue, using the following packages:
Everything works fine when using the memory connector, but with mongo I can't find entities via relationships because the ID is saved in mongodb as a string.
bajtos commentedon Nov 10, 2014
@raymondfeng @fabien could you PTAL? You know more about juggler and connector internals than I do.
danielbodnar commentedon Nov 30, 2014
This may not be the same exact issue, but it's similar.
I ran into this relations ObjectID issue whenever PUTing or POSTing an object with one of the belongsTo still attached. So for example, say I did a Car.find({include:'owner'}), then modified the car without detaching / deleting the owner object, the datasource juggler would replace the fk with a string and remove the object.
My hacky solution was to create a beforeRemote hook for upsert for every model via a bootscript, like so:
Can anyone recommend a more efficient way of doing this?
jorgeramos commentedon Jan 12, 2015
Hi @danielbodnar - your code above helped me solve a similar issue. The code below iterates through all model relations converting them to proper mongo ObjectIDs if the data property is either an object with an id or just a string. It also fixes dates that were getting stored as string instead of ISODate. As it's going through the data object, it checks to see if the prop is a nested model and tries to fix it's properties as well. You can choose which remote methods to affect by editing the array with method names. I'm not sure what's the root of the problem or if this is the best solution but I hope it helps...
raymondfeng commentedon Jan 12, 2015
Sorry I missed it. I'll check soon.
jorgeramos commentedon Jan 12, 2015
i updated the code slightly to provide proper foreign key when the relation is defined without a custom foreign key. Also, it would be great if the date and object id ran through a regex before attempting to process. At the very least there should be some try-and-catch to prevent the app from erroring out...
danielbodnar commentedon Jan 12, 2015
Thanks @jorgeramos, looks much more elegant.
raymondfeng commentedon Jan 12, 2015
The type of a foreign key property will be updated to match to the corresponding primary key by LoopBack. I'll check the add() method to see why the value is stored as string in the joined collection.
Tarabyte commentedon Feb 2, 2015
Any news on the issue?
I have almost the same problem with belongsTo relation. Post belongsTo Category.
categoryIdis saved asObjectId. Which is an expected behaviour of course.categoryIdis converted to aString. Which results in various problems later when one needs to query posts by category.jrhone commentedon Feb 3, 2015
For what it's worth, AccessToken id's are currently generated as Strings instead of ObjectIds.. different bug?
{
"ttl" : 1209600,
"created" : ISODate("2014-12-01T22:50:18.752Z"),
"userId" : ObjectId("5464f1f8df5ea75545c39d1c"),
"_id" : "uqdHTCgre9LD3XQHeH2BUAkW8iVxWSsxGEKmKaAjjRBm9nxNheRSTAbzn8lxcaHZ"
}
raymondfeng commentedon Feb 3, 2015
Access token ids are hashed keys. They are not object ids.
Thanks,
Raymond Feng
Co-Founder and Architect @ StrongLoop, Inc.
StrongLoop http://strongloop.com/ makes it easy to develop APIs http://strongloop.com/mobile-application-development/loopback/ in Node, plus get DevOps capabilities http://strongloop.com/node-js-performance/strongops/ like monitoring, debugging and clustering.
49 remaining items
fabien commentedon Mar 10, 2017
This has been fixed recently, but it only works out-of-the-box with new Loopback apps, because the data has been stored in the legacy format in existing apps. You'd need to migrate that data yourself in that case.
hyperd commentedon Mar 24, 2017
@codejie as @fabien pointed out the
strictObjectIDCoercionis now available for RoleMapping model in new applications created usinglb,apicetc.In my case I had to add it manually to
model-config.jsonwhich looks like this:HugoPoi commentedon May 17, 2017
@hyperd I recently upgrade loopback stuff and get problems with RoleMapping principalId type.
From
To
In RoleMapping collection principalId was string type. After upgrading RoleMapping doesn't work. User wasn't mapped to the correct Role. I migrate principalId to ObjectId type in RoleMapping collection and all work again without changes in config. And I haven't
strictObjectIDCoercionset to true.This is a breaking change, but I haven't identified where and when this occurs.
dsl400 commentedon Jun 5, 2017
+1
stale commentedon Mar 11, 2020
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 commentedon Mar 26, 2020
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
CODEOWNERSfile at the top-level of this repository.