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

Can not .create() values with model stored in MongoDb having "one-way association" with another model stored in MySQL #4484

Closed
maheshwarishivam opened this issue Aug 6, 2018 · 5 comments

Comments

@maheshwarishivam
Copy link

Sails version: v1.0.2
Node version: v8.1.2
NPM version: 4.2.0
DB adapter name: sails-mongo
DB adapter version: ^1.0.1
Operating system: Mac OSX


Hi,

This seems to be a bug in sails-mongo adapter v 1.0.1.

Sails Mongo Adapter seems to assume that the foreign keys (One Way Association defined using "model" attribute) have to be "Mongo Keys" even if the referenced model lives in "MySQL".

Here is how to replicate:

I have a Users model stored in MongoDb and a UserTypes model stored in MySQL:

Users.js:

module.exports = {

  datastore: 'mongoDb',
  attributes: {
   id: {type: 'string', columnName: '_id', required: false},

    name: {
      type: 'string',
      required: false,
      allowNull: true,
      maxLength: 50,
      minLength: 3
    },

    userType: {model: 'usertypes', required: true},
  }
};

and UserTypes.js:

module.exports = {

  datastore: 'default',
  attributes: {

    id: {type: 'number', autoIncrement: true},

    label: {
      type: 'string',
      maxLength: 255,
      required: true,
      unique: true
    },
    createdAt: false,
    updatedAt: false,
    users: {collection: 'users', via: 'userType'}
  }
};

where default datastore is MySQL and mongoDb datastore is MongoDb.

When I try to create a new User:

Users.create({name: "Shivam", userType: 1})

I get following error:

{ AdapterError: Unexpected error from database adapter: Invalid replacement foreign key value provided for association (`userType`).  Cannot interpret `1` as a Mongo id.


(Usually, this is the result of a bug in application logic.)
For more info on Mongo ids, see:
• https://docs.mongodb.com/manual/reference/bson-types/#objectid
• http://sailsjs.com/support


    at _afterPotentiallyFinding (/My_Project_Root//node_modules/waterline/lib/waterline/methods/find-or-create.js:258:17)
    at _afterPotentiallyRunningAfterLC (/My_Project_Root//node_modules/waterline/lib/waterline/methods/find-one.js:328:20)
    at _maybeRunAfterLC (/My_Project_Root//node_modules/waterline/lib/waterline/methods/find-one.js:322:20)
    at _afterFetchingRecords (/My_Project_Root//node_modules/waterline/lib/waterline/methods/find-one.js:324:13)
    at _afterGettingPopulatedPhysicalRecords (/My_Project_Root//node_modules/waterline/lib/waterline/utils/query/help-find.js:754:12)
    at /My_Project_Root//node_modules/waterline/lib/waterline/utils/query/help-find.js:151:16
    at Object.success (/My_Project_Root//node_modules/sails-mongo/lib/private/do-with-connection.js:254:18)
    at /My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:1354:30
    at _tryToRunCb (/My_Project_Root//node_modules/parley/lib/private/Deferred.js:542:20)
    at /My_Project_Root//node_modules/parley/lib/private/Deferred.js:755:16
    at /My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:966:24
    at Function.handlerCbs.success (/My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:826:26)
    at Object.fn (/My_Project_Root//node_modules/sails-mongo/lib/private/machines/release-connection.js:77:18)
    at wrapper (/My_Project_Root//node_modules/@sailshq/lodash/lib/index.js:3275:19)
    at Deferred.parley.now.flaverr.name [as _handleExec] (/My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:1068:17)
    at Deferred.exec (/My_Project_Root//node_modules/parley/lib/private/Deferred.js:679:10)
    at Deferred.switch (/My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:1306:16)
    at /My_Project_Root//node_modules/sails-mongo/lib/private/do-with-connection.js:242:92
    at /My_Project_Root//node_modules/sails-mongo/lib/private/do-with-connection.js:123:18
    at Object.success (/My_Project_Root//node_modules/sails-mongo/lib/private/build-std-adapter-method.js:61:47)
    at /My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:1354:30
    at _tryToRunCb (/My_Project_Root//node_modules/parley/lib/private/Deferred.js:520:18)
  cause: 
   { AdapterError: Unexpected error from database adapter: Invalid replacement foreign key value provided for association (`userType`).  Cannot interpret `1` as a Mongo id.
   (Usually, this is the result of a bug in application logic.)
   For more info on Mongo ids, see:
   • https://docs.mongodb.com/manual/reference/bson-types/#objectid
   • http://sailsjs.com/support
       at _afterPotentiallyFinding (/My_Project_Root//node_modules/waterline/lib/waterline/methods/find-or-create.js:258:17)
       at _afterPotentiallyRunningAfterLC (/My_Project_Root//node_modules/waterline/lib/waterline/methods/find-one.js:328:20)
       at _maybeRunAfterLC (/My_Project_Root//node_modules/waterline/lib/waterline/methods/find-one.js:322:20)
       at _afterFetchingRecords (/My_Project_Root//node_modules/waterline/lib/waterline/methods/find-one.js:324:13)
       at _afterGettingPopulatedPhysicalRecords (/My_Project_Root//node_modules/waterline/lib/waterline/utils/query/help-find.js:754:12)
       at /My_Project_Root//node_modules/waterline/lib/waterline/utils/query/help-find.js:151:16
       at Object.success (/My_Project_Root//node_modules/sails-mongo/lib/private/do-with-connection.js:254:18)
       at /My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:1354:30
       at _tryToRunCb (/My_Project_Root//node_modules/parley/lib/private/Deferred.js:542:20)
       at /My_Project_Root//node_modules/parley/lib/private/Deferred.js:755:16
       at /My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:966:24
       at Function.handlerCbs.success (/My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:826:26)
       at Object.fn (/My_Project_Root//node_modules/sails-mongo/lib/private/machines/release-connection.js:77:18)
       at wrapper (/My_Project_Root//node_modules/@sailshq/lodash/lib/index.js:3275:19)
       at Deferred.parley.now.flaverr.name [as _handleExec] (/My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:1068:17)
       at Deferred.exec (/My_Project_Root//node_modules/parley/lib/private/Deferred.js:679:10)
       at Deferred.switch (/My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:1306:16)
       at /My_Project_Root//node_modules/sails-mongo/lib/private/do-with-connection.js:242:92
       at /My_Project_Root//node_modules/sails-mongo/lib/private/do-with-connection.js:123:18
       at Object.success (/My_Project_Root//node_modules/sails-mongo/lib/private/build-std-adapter-method.js:61:47)
       at /My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:1354:30
       at _tryToRunCb (/My_Project_Root//node_modules/parley/lib/private/Deferred.js:520:18)
     name: 'AdapterError',
     adapterMethodName: 'create',
     modelIdentity: 'users',
     raw: 
      { Error: Invalid replacement foreign key value provided for association (`userType`).  Cannot interpret `1` as a Mongo id.
      (Usually, this is the result of a bug in application logic.)
      For more info on Mongo ids, see:
      • https://docs.mongodb.com/manual/reference/bson-types/#objectid
      • http://sailsjs.com/support
          at /My_Project_Root//node_modules/sails-mongo/lib/private/machines/private/reify-values-to-set.js:95:35
          at /My_Project_Root//node_modules/@sailshq/lodash/lib/index.js:3253:15
          at baseForOwn (/My_Project_Root//node_modules/@sailshq/lodash/lib/index.js:2223:14)
          at /My_Project_Root//node_modules/@sailshq/lodash/lib/index.js:3223:18
          at Function.<anonymous> (/My_Project_Root//node_modules/@sailshq/lodash/lib/index.js:3526:13)
          at reifyValuesToSet (/My_Project_Root//node_modules/sails-mongo/lib/private/machines/private/reify-values-to-set.js:68:5)
          at Object.fn (/My_Project_Root//node_modules/sails-mongo/lib/private/machines/create-record.js:58:7)
          at wrapper (/My_Project_Root//node_modules/@sailshq/lodash/lib/index.js:3275:19)
          at Deferred.parley.now.flaverr.name [as _handleExec] (/My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:1068:17)
          at Deferred.exec (/My_Project_Root//node_modules/parley/lib/private/Deferred.js:679:10)
          at Deferred.switch (/My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:1306:16)
          at Object.during (/My_Project_Root//node_modules/sails-mongo/lib/private/build-std-adapter-method.js:74:18)
          at _makeCallToDuringFn (/My_Project_Root//node_modules/sails-mongo/lib/private/do-with-connection.js:109:17)
          at /My_Project_Root//node_modules/sails-mongo/lib/private/do-with-connection.js:128:7
          at Object.success (/My_Project_Root//node_modules/sails-mongo/lib/private/do-with-connection.js:91:16)
          at /My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:1354:30
          at _tryToRunCb (/My_Project_Root//node_modules/parley/lib/private/Deferred.js:542:20)
          at /My_Project_Root//node_modules/parley/lib/private/Deferred.js:755:16
          at /My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:966:24
          at Function.handlerCbs.success (/My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:826:26)
          at Object.fn (/My_Project_Root//node_modules/sails-mongo/lib/private/machines/get-connection.js:61:18)
          at wrapper (/My_Project_Root//node_modules/@sailshq/lodash/lib/index.js:3275:19) code: 'E_CANNOT_INTERPRET_AS_OBJECTID' } },
  isOperational: true,
  adapterMethodName: 'create',
  modelIdentity: 'users',
  raw: 
   { Error: Invalid replacement foreign key value provided for association (`userType`).  Cannot interpret `1` as a Mongo id.
   (Usually, this is the result of a bug in application logic.)
   For more info on Mongo ids, see:
   • https://docs.mongodb.com/manual/reference/bson-types/#objectid
   • http://sailsjs.com/support
       at /My_Project_Root//node_modules/sails-mongo/lib/private/machines/private/reify-values-to-set.js:95:35
       at /My_Project_Root//node_modules/@sailshq/lodash/lib/index.js:3253:15
       at baseForOwn (/My_Project_Root//node_modules/@sailshq/lodash/lib/index.js:2223:14)
       at /My_Project_Root//node_modules/@sailshq/lodash/lib/index.js:3223:18
       at Function.<anonymous> (/My_Project_Root//node_modules/@sailshq/lodash/lib/index.js:3526:13)
       at reifyValuesToSet (/My_Project_Root//node_modules/sails-mongo/lib/private/machines/private/reify-values-to-set.js:68:5)
       at Object.fn (/My_Project_Root//node_modules/sails-mongo/lib/private/machines/create-record.js:58:7)
       at wrapper (/My_Project_Root//node_modules/@sailshq/lodash/lib/index.js:3275:19)
       at Deferred.parley.now.flaverr.name [as _handleExec] (/My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:1068:17)
       at Deferred.exec (/My_Project_Root//node_modules/parley/lib/private/Deferred.js:679:10)
       at Deferred.switch (/My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:1306:16)
       at Object.during (/My_Project_Root//node_modules/sails-mongo/lib/private/build-std-adapter-method.js:74:18)
       at _makeCallToDuringFn (/My_Project_Root//node_modules/sails-mongo/lib/private/do-with-connection.js:109:17)
       at /My_Project_Root//node_modules/sails-mongo/lib/private/do-with-connection.js:128:7
       at Object.success (/My_Project_Root//node_modules/sails-mongo/lib/private/do-with-connection.js:91:16)
       at /My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:1354:30
       at _tryToRunCb (/My_Project_Root//node_modules/parley/lib/private/Deferred.js:542:20)
       at /My_Project_Root//node_modules/parley/lib/private/Deferred.js:755:16
       at /My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:966:24
       at Function.handlerCbs.success (/My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:826:26)
       at Object.fn (/My_Project_Root//node_modules/sails-mongo/lib/private/machines/get-connection.js:61:18)
       at wrapper (/My_Project_Root//node_modules/@sailshq/lodash/lib/index.js:3275:19)


 code: 'E_CANNOT_INTERPRET_AS_OBJECTID' } }

However, .populate() works just fine:

Users.findOne('USER_ID_HERE').populate('userType')

yields the following result:

{
  id: "USER_ID_HERE",
  name: "Shivam",
  userType: {
    id: 1,
    label: "Student"
  }
}

Am I doing something wrong or is this actually a bug?

@sailsbot
Copy link

sailsbot commented Aug 6, 2018

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

@maheshwarishivam
Copy link
Author

I have also posted a Question on Stack Overflow explaining the same:

SailsJS 1.0 : Error in .create() for a model in Mongo with One Way Association to a model stored in MySQL

@maheshwarishivam
Copy link
Author

UPDATE:

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

However, using a model setting which is ONLY TO BE USED WITH SAILS-MONGO Adapter for a Model using sails-mysql adapter is strange as well as un-documented.

Now, I'm wondering, if this is bug or an undocumented feature?

@maheshwarishivam
Copy link
Author

Thanks for the reply.

Now I see!

This is a duplicate of #4367

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

No branches or pull requests

3 participants