balderdashy / sails

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

One-to-many create parent + childs fails #6907

Open lreyessandoval opened 4 years ago

lreyessandoval commented 4 years ago

Node version: v12.13.0 Sails version (sails): 1.2.3 DB adapter & version : sails-mysql@5.7.23-cll-lve


I have a model User (Parent):

/**
 * User.js
 *
 * @description :: A model definition represents a database table/collection.
 * @docs        :: https://sailsjs.com/docs/concepts/models-and-orm/models
 */

module.exports = {
  attributes: {
    firstName: {
      type: 'string'
    },
    lastName: {
      type: 'string'
    },

    // Add a reference to Pets
    pets: {
      collection: 'pet',
      via: 'owner'
    }
  }
};

And Model Pet (Child):

/**
 * Pet.js
 *
 * @description :: A model definition represents a database table/collection.
 * @docs        :: https://sailsjs.com/docs/concepts/models-and-orm/models
 */

module.exports = {
  attributes: {
    breed: {
      type: 'string'
    },
    type: {
      type: 'string'
    },
    name: {
      type: 'string'
    },

    // Add a reference to User
    owner: {
      model: 'user'
    }
  }
};

Find/FindAll populating works fine, but when I try to create a header (User) and details (Pet) an error appears.

**POST: http://localhost:1337/User ** 
{
    "firstName": "Luis",
    "lastName": "Reyes",
    "pets": [
        {
            "breed": "labrador",
            "type": "dog",
            "name": "Princesa"
        },
        {
            "breed": "labrador",
            "type": "dog",
            "name": "Luna"
        }
    ]
}

This is the error that returns:

UsageError: Invalid criteria.
Details:
Could not use the provided `where` clause. Could not filter by `id`: Invalid item within `in` modifier array. Does not
match the declared data type of the corresponding attribute. 1 error validating value:
• Specified value (a dictionary: { breed: 'labrador', type: 'dog', name: 'Princesa' }) doesn't match the expected type:
'number'

at D:\2.- Desarrollo\poc\backend\apierp\node_modules\sails\lib\hooks\blueprints\actions\create.js:53:45
at D:\2.- Desarrollo\poc\backend\apierp\node_modules\sails\node_modules\async\dist\async.js:2489:9
at replenish (D:\2.- Desarrollo\poc\backend\apierp\node_modules\sails\node_modules\async\dist\async.js:946:17)
at iterateeCallback (D:\2.- Desarrollo\poc\backend\apierp\node_modules\sails\node_modules\async\dist\async.js:931:17)
at D:\2.- Desarrollo\poc\backend\apierp\node_modules\sails\node_modules\async\dist\async.js:906:16
at D:\2.- Desarrollo\poc\backend\apierp\node_modules\sails\node_modules\async\dist\async.js:2491:13
at D:\2.- Desarrollo\poc\backend\apierp\node_modules\sails\lib\hooks\blueprints\actions\create.js:71:14
at D:\2.- Desarrollo\poc\backend\apierp\node_modules\sails\node_modules\async\dist\async.js:2489:9
at replenish (D:\2.- Desarrollo\poc\backend\apierp\node_modules\sails\node_modules\async\dist\async.js:946:17)
at iterateeCallback (D:\2.- Desarrollo\poc\backend\apierp\node_modules\sails\node_modules\async\dist\async.js:931:17)
at D:\2.- Desarrollo\poc\backend\apierp\node_modules\sails\node_modules\async\dist\async.js:906:16
at D:\2.- Desarrollo\poc\backend\apierp\node_modules\sails\node_modules\async\dist\async.js:2491:13
at D:\2.- Desarrollo\poc\backend\apierp\node_modules\sails\lib\hooks\blueprints\actions\create.js:71:14
at D:\2.- Desarrollo\poc\backend\apierp\node_modules\sails\node_modules\async\dist\async.js:2489:9
at replenish (D:\2.- Desarrollo\poc\backend\apierp\node_modules\sails\node_modules\async\dist\async.js:946:17)
at iterateeCallback (D:\2.- Desarrollo\poc\backend\apierp\node_modules\sails\node_modules\async\dist\async.js:931:17)
at D:\2.- Desarrollo\poc\backend\apierp\node_modules\sails\node_modules\async\dist\async.js:906:16
at D:\2.- Desarrollo\poc\backend\apierp\node_modules\sails\node_modules\async\dist\async.js:2491:13
at D:\2.- Desarrollo\poc\backend\apierp\node_modules\sails\lib\hooks\blueprints\actions\create.js:71:14
at D:\2.- Desarrollo\poc\backend\apierp\node_modules\sails\node_modules\async\dist\async.js:2489:9
at replenish (D:\2.- Desarrollo\poc\backend\apierp\node_modules\sails\node_modules\async\dist\async.js:946:17)
at iterateeCallback (D:\2.- Desarrollo\poc\backend\apierp\node_modules\sails\node_modules\async\dist\async.js:931:17)
at D:\2.- Desarrollo\poc\backend\apierp\node_modules\sails\node_modules\async\dist\async.js:906:16
at D:\2.- Desarrollo\poc\backend\apierp\node_modules\sails\node_modules\async\dist\async.js:2491:13
at D:\2.- Desarrollo\poc\backend\apierp\node_modules\sails\lib\hooks\blueprints\actions\create.js:71:14
at D:\2.- Desarrollo\poc\backend\apierp\node_modules\sails\node_modules\async\dist\async.js:2489:9
at replenish (D:\2.- Desarrollo\poc\backend\apierp\node_modules\sails\node_modules\async\dist\async.js:946:17)
at iterateeCallback (D:\2.- Desarrollo\poc\backend\apierp\node_modules\sails\node_modules\async\dist\async.js:931:17)
sailsbot commented 4 years ago

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

whichking commented 4 years ago

Hey, @lreyessandoval! I believe the functionality you're looking for isn't supported in Sails v1. Per the upgrade guide:

The .create(), .update() and .add() model methods no longer support creating a new “child” record to link immediately to a new or existing parent. For example, given a User model with a singular association to an Animal model through an attribute called pet, it is not possible to set pet to a dictionary representing values for a brand new Animal (aka a “nested create”). Instead, create the new Animal first and use its primary key to set pet when creating the new User.

lreyessandoval commented 4 years ago

Thanks, I will look for a workarround for this case ...

Is there a date to release this functionality in a next release?

whichking commented 4 years ago

@lreyessandoval—from what I understand, this functionality was supported in older (pre-v1) versions of Sails. I don't think that there are any plans to revive it for future versions.