Search code examples
sails.jsone-to-manywaterlineapiblueprint

Sails 1.0 cannot access autogenerated associated entity routes


I have an association between Client and Budget as follows:

//Client.js
module.exports = {
  primaryKey: 'id',

  attributes: {
    id: {
      type: 'number',
      unique: true,
      autoIncrement: true
    },
    name: {
      type: 'string'
    },
    phone: {
      type: 'string',
      unique: true
    },
    email: {
      type: 'string',
      unique: true
    },
    budgets: {
      collection: 'budget',
      via: 'client'
    },
  }
};


// Budget.js
module.exports = {
  primaryKey: 'id',

  attributes: {
    id: {
      type: 'number',
      unique: true,
      autoIncrement: true
    },
    client: {
      model: 'client'
    },
    budgetItems: {
      type: 'json'
    }
  }
};

So, POST is working for both entities so I can create both of them, but:

 GET /budget/1

returns the budget and the id of the associated client.

 GET /budget/1/client

returns the client id without populating(as i've seen in documentation it should be populated).

 GET /client/1

returns the client attributes and there is not a field related with budgets.

 GET /client/1/budgets

returns 404 NOT FOUND

I'm following this and this official documentation

So what I could be missing?

Is just generating one direction associations, and I've compared with official documentation and third party examples and my code looks fine.

Thanks in advance!

Update:

I still looking for troubles and if I run sails with --silly option it shows there is the following route available:

Binding route :: get /client/:parentid/budgets POLICY: localize
Binding route :: get /client/:parentid/budgets POLICY: isauth
Binding route :: get /client/:parentid/budgets BLUEPRINT: populate

but if I try to access returns a 404 Not Found and console shows the following error, thrown by populate.js from Sails core code:

verbo: In populate blueprint action: Specified parent record (1) does not have a budgets.

Update2:

Debugging with sails console I've seen the association is generated properly. Given Client.findOne({id: 1}).populate('budgets').then((client)=>{console.log(client)}) print the client attributes and the associated Budgets but still return 404 Not Found when: GET /client/1/budgets


Solution

  • thanks to SailsJS team we've found the problem and it was related with a third party package and just had to remove it from my project. It was sails-hook-deep-orm who's owner has been warned. I hope someone with the same issue will reach this post.

    Thanks u all anyway!!

    The issue is available there