balderdashy / sails

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

Archive by id or _id fails in MongoDb #4436

Open lcavero opened 6 years ago

lcavero commented 6 years ago

Sails version: 1.0.2 Node version: v8.10.0 NPM version: 3.5.2 DB adapter name: sails-mongo DB adapter version: 1.0.1 Operating system: "Ubuntu 18.04"


Hello, I have detected that the "archive" method fails in MongoDb when you set the config property "schema" to true and try to do a "soft delete" for id or _id.

fn: async function (inputs, exits) {
    // That line fails
    await SharedDocument.archive({id: "5b0fae71d8785b29956bf65f"})

   // That line fails too
    await SharedDocument.archive({_id: "5b0fae71d8785b29956bf65f"})

    return exits.success({ status: "Ok" });
  }

With either of the two cases (id or _id) the following error occurs

Could not use the provided `where` clause.  Could not filter by `_id`: `_id` is not a recognized attribute for this model (`shareddocument`).  And since the model declares `schema: true`, this is not allowed.

Then, if you show the database, there are two copies of the record, one in SharedDocument, and other in archive, so I guess sails is failing to delete the original. This only happens if the model config parameter "schema" is set to true

Thank you guys!

sailsbot commented 6 years ago

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

johnabrams7 commented 5 years ago

Hi @lcavero, thanks for providing info about this duplicate phenomenon when the model config parameter "schema" is set to true.

johnabrams7 commented 5 years ago

@lcavero Feel free to submit a PR for sails-mongo to patch this and we'll take a look.

simplewilliam commented 5 years ago

Hi, has there been any patch for this error yet? I am trying to use Waterline with Mongo DB but this issue still pops up when trying to archive.

simplewilliam commented 5 years ago

Just to make a note on my work around for anyone running into this error. I created a helper function that calls archive/archiveOne and catches the error where it checks if it was added to archive and if so call the destroy method.

johnabrams7 commented 5 years ago

@simplewilliam - Thanks for taking the time to provide a workaround - any example code or repo is further welcome. Currently, we have several plans in store for sails-mongo, I'll bring this one up with the team.

simplewilliam commented 5 years ago

@johnabrams7 - Thanks for looking into it, the following code can be placed within a helper function to act as an alternative for the archive function when trying to archive by id

    fn: async function(inputs, exits) {
        const { model, id } = inputs;
        let archivedDocument;
        try {
            archivedDocument = await model.archiveOne({ id });
        } catch (err) {
           var archived = await Archive.find({
           originalRecordId: id,});
           if (archived) {
               archivedDocument = await model.destroyOne({ id });
           }
        }
        return exits.success(archivedDocument);
  }
solovieff commented 4 years ago

Same here, can not do:

Model.archiveOne({id: id})

Got the following error:

Could not use the providedwhereclause. Could not filter by_id:_idis not a recognized attribute for this model

johnabrams7 commented 4 years ago

@solovieff We tested with the latest version of the adapter and it worked for us. Could you create a repo reproducing the error?