Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoDB adapter doesn't recognise FKs to mysql table correctly #4367

Open
mp3por opened this issue Apr 20, 2018 · 8 comments
Open

MongoDB adapter doesn't recognise FKs to mysql table correctly #4367

mp3por opened this issue Apr 20, 2018 · 8 comments

Comments

@mp3por
Copy link

mp3por commented Apr 20, 2018

Sails version: sails@1.0.1
Node version: 8.9.4
NPM version: 5.6.0
DB adapter name: sails-mongo
DB adapter version: sails-mongo@1.0.0
Operating system: macOS 10.13.3


Hello,

I have a reference from my mongo model ( Client ) to a mysql model via user: { model: 'User', required: true } and User is in mysql database. I then want to create a Client and supply {user: 1} the adapter throws

Error: Invalid replacement foreign key value provided for association (user). Cannot interpret 1 as a Mongo id.

REPO: git@bitbucket.org:mp3por/sails-bug-2.git

Please advice :)

@sailsbot
Copy link

@mp3por Thanks for posting, we'll take a look as soon as possible.


For help with questions about Sails, click here. If you’re interested in hiring @sailsbot and her minions in Austin, click here.

@mp3por
Copy link
Author

mp3por commented Apr 20, 2018

This also doesn't work if the id of the linked mysql model is UUID.

I managed to hack it for UUID via user : { type:'string'} and for ID via user : { type: 'number' }.

@mikermcneil
Copy link
Member

@mp3por thanks for the heads up- I think I see why this is happening

image

Will look into it ASAP. Your workaround makes sense in the mean time (of course it means you won't be able to rely on .populate())

@mikermcneil
Copy link
Member

mikermcneil commented Apr 23, 2018

Here's the code in sails-mongo that's specifically wrong:
https://github.com/balderdashy/sails-mongo/blob/18d0f31df0da0659c93bd5f59cdc4d422ab596a3/lib/private/machines/private/reify-values-to-set.js#L88-L99

@mp3por Also in the mean time, if you have a moment to take a pass at a PR, I'd be happy to review and hopefully merge. I think what we need there is to add another disjunct checking if the foreign key is pointed at another model powered by the current sails-mongo adapter, and if so, skip the check. Please just comment here with that PR if you get to it

(Also, you'll notice in that code that there's another workaround hack you could use involving meta-- though you shouldn't have to -- this is definitely something that should work out of the box)

@maheshwarishivam
Copy link

maheshwarishivam commented Aug 6, 2018

Another possible workaround without changing the Model Structure (NOT a fix):

Setting dontUseObjectIds: true for the Model (which is stored in MySQL) seems to fix the issue.

In the example in OP, set this for User model.

Bonus: .populate() will work too!

@mikermcneil
Copy link
Member

mikermcneil commented Aug 6, 2018

@maheshwarishivam 👍 thanks for the follow-up!

@streleck this is related to the other issue we were talking about this morning. (If you've still got that link handy, would make sense to backlink here) (Never mind, just noticed @maheshwarishivam already did 👍)

@ghost
Copy link

ghost commented Nov 14, 2018

@maheshwarishivam I'm using sails@1.0.2 and I have the same issue. Does that label closed means it is already available in new version?

node v8.12.0
npm 6.4.1
sails-mongo@1.0.1
sails-mysql@1.0.0
ubuntu 16.04 LTS

@johnabrams7
Copy link
Contributor

johnabrams7 commented Apr 24, 2019

@ghost - The referenced issue was closed because for being relatively duplicate of this one -however it has a lot of useful info so it's definitely worth checking out. @maheshwarishivam Thanks for the workaround!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

5 participants