1

My sails application works well while connected to mysql database. I however had the need to switch to mongoDB as it is a requirement for the project and here comes the issues I face.

My models have some relationships for it to work and to achieve this, I had to make some modifications which include the following: I used uuid to set the primary keys (id) for each model (The id was automatically generated by mysql before). When I however try to submit requests to the server I experience the error:

AdapterError: Unexpected error from database adapter: Invalid primary key value provided for `id`.  Cannot interpret `9456b206-ebcf-4a6d-b81c-93964c027f04` as a Mongo id.
(Usually, this is the result of a bug in application logic.)

Here is a sample of one of my models - picked.js:

module.exports = {
  
  attributes: {
    userId: {
      type: 'string',
      required: true,
      //unique: true,
    },
    comment:{
      type: 'string',
      required: true
    },
    createdBy:{
      type: 'string',
      required: true
    },
    info: {
      model: 'personalinfo' 
    },
    admin: {
      model: 'admin'
    },

    id:{
      type: 'string',
    },

  },

};
mykoman
  • 1,715
  • 1
  • 19
  • 33
  • Sounds like you are trying to use empty strings as ids, which is not allowed. – D. SM Jun 07 '20 at 03:14
  • No, I wasn't using empty strings, if you check the body of my question, you would see the strings I tried adding. I have also update my question – mykoman Jun 07 '20 at 06:18
  • Does this answer your question? [Sails v1.0: error while using custom primary key with mongo](https://stackoverflow.com/questions/43003762/sails-v1-0-error-while-using-custom-primary-key-with-mongo) – Joe Jun 07 '20 at 06:39
  • What were the id values you used then? – D. SM Jun 07 '20 at 06:48
  • https://stackoverflow.com/questions/43003762/sails-v1-0-error-while-using-custom-primary-key-with-mongo looks like the correct answer. – D. SM Jun 07 '20 at 07:01

1 Answers1

0

I found a short way to resolve the issue. I didn't have to use uuid to set primary keys. Sails already made using the snippet below in config.models.js to connect with mongo db and set primary keys conveniently.

attributes: {
  ...
  //id: { type: 'number', autoIncrement: true, },
  id: { type: 'string', columnName: '_id' },
  ...
}
mykoman
  • 1,715
  • 1
  • 19
  • 33