balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.84k stars 1.95k forks source link

sails-mongo: errors using two collections with the same name in two databases #7225

Open benoitbzl opened 2 years ago

benoitbzl commented 2 years ago

Node version: 14.x Sails version (sails): 1.5.2 ORM hook version (sails-hook-orm): 4.0.1 Sockets hook version (sails-hook-sockets): Organics hook version (sails-hook-organics): Grunt hook version (sails-hook-grunt): Uploads hook version (sails-hook-uploads): DB adapter & version (e.g. sails-mysql@5.55.5): sails-mongo@2.0.0 Skipper adapter & version (e.g. skipper-s3@5.55.5):


We were facing some rare errors with db corruptions and warnings in the sails console:

2022-05-07T14:26:23.934321633Z 2022-05-07T14:26:23.934Z warn 2022-05-07T14:26:23.934336134Z Warning: Records sent back from a database adapter should always have a valid property 2022-05-07T14:26:23.934342234Z that corresponds with the primary key attribute (id). But in this result set, 2022-05-07T14:26:23.934347234Z after transforming columnNames back to attribute names for model job, 2022-05-07T14:26:23.934352034Z there is a record with a missing or invalid id. 2022-05-07T14:26:23.934357134Z Record: 2022-05-07T14:26:23.934361834Z 2022-05-07T14:26:23.934366734Z 601196002777e1002ab23f55 2022-05-07T14:26:23.934371534Z

It turns out that the origin of the issue is that we have two models in two different datastores (mongo database) having the same collection names (tableName property of a model). This is perfectly valid from mongo perspective, and I don't find any limitation about the tableName in the sails documentation.

Looking at the sails-mongo code, for example for the find() request, I think the issue is there:

https://github.com/balderdashy/sails-mongo/blob/02d3744d4568d126c9cbef5df764af1b2194ff7e/lib/private/machines/find-records.js#L46

The model is found in the set of models based only on the table name: it does not take into account the datastore name. So in our case, this set contains 2 models (with different names), but having identical values for the property tableName. Depending on order of the models in the set, a different model is picked by the code.

sailsbot commented 2 years ago

@benoitbzl Thanks for posting! We'll take a look as soon as possible.

In the mean time, there are a few ways you can help speed things along:

Please remember: never post in a public forum if you believe you've found a genuine security vulnerability. Instead, disclose it responsibly.

For help with questions about Sails, click here.

eashaw commented 2 years ago

Thanks for posting this @benoitbzl, I'll try reproducing this.