Sails version: 0.12.11
Node version: v7.0.0
NPM version: 4.0.3
Operating system: macOS Sierra
/model1/:id/model2/:id returns an array of objects (same response as /model1/:id/model2/ (Not expected)./model2/:id returns just one object as expected (Expected).
Model1 has a one to many relationship with Model2.
More specifically, I have 3 sails models with the following one to many relationships:
- User
- Account
- Transaction
- Transaction...
- Account ...
When I GET /users/:id/accounts/:id, I get an array like so
[
{
"user": "d7156354-6587-4614-b92b-7e8091e3311c",
"id": "e7c41cc3-7b98-482e-8aa3-2cdd42eab3a1"
}
]
I expected this to return a single Account object, and not an array containing just one Account object.
I get a single Account object as expected when I GET /accounts/:id
I also get a single User object as expected when I GET /users/:id/,
My model files look like so
// User.js
module.exports = {
autoCreatedAt: false,
autoUpdatedAt: false,
attributes: {
id: {
type: 'string',
unique: true,
primaryKey: true,
defaultsTo: () => uuid.v4(),
},
name: {
type: 'string',
},
email: {
type: 'string',
unique: true,
},
accounts: {
collection: 'account',
via: 'user',
}
}
};
// Account.js
module.exports = {
autoCreatedAt: false,
autoUpdatedAt: false,
attributes: {
id: {
type: 'string',
unique: true,
primaryKey: true,
defaultsTo: () => uuid.v4(),
},
date: {
type: 'date',
defaultsTo: () => new Date(),
},
amount: {
type: 'float',
defaultsTo: 0.0,
},
account: {
model: 'account',
},
type: {
type: 'string',
enum: ['credit', 'debit'],
defaultsTo: 'credit',
},
description: {
type: 'string',
defaultsTo: () => `Transaction at ${new Date()}`
}
}
};
// Transaction.js
module.exports = {
autoCreatedAt: false,
autoUpdatedAt: false,
attributes: {
id: {
type: 'string',
unique: true,
primaryKey: true,
defaultsTo: () => uuid.v4(),
},
date: {
type: 'date',
defaultsTo: () => new Date(),
},
amount: {
type: 'float',
defaultsTo: 0.0,
},
account: {
model: 'account',
},
type: {
type: 'string',
enum: ['credit', 'debit'],
defaultsTo: 'credit',
},
description: {
type: 'string',
defaultsTo: () => `Transaction at ${new Date()}`
}
}
};
This looks like a bug, unless I'm doing something wrong.
Hi @G2Jose! It looks like you missed a step or two when you created your issue. Please edit your comment (use the pencil icon at the top-right corner of the comment box) and fix the following:
As soon as those items are rectified, post a new comment (e.g. “Ok, fixed!”) below and we'll take a look. Thanks!
*If you feel this message is in error, or you want to debate the merits of my existence (sniffle), please contact [email protected]
Ok, fixed!
@G2Jose thanks for the detailed explanation! Digging in and will get back to you ASAP-- I agree on the blueprint routes themselves seeming unexpected here vs what's documented.
In the mean time, just to point this out in case it's helpful: Any time you're doing GET /users/1/accounts/2, you can actually just shave off the front and be more concise-- i.e. just do GET /accounts/2 (because 2 is a unique id across _all_ account records).
Okay, these routes have been removed in Sails 1.0. As @mikermcneil said, you can get the same functionality by using the more direct URL (e.g. /accounts/2).
It doesn't look like these routes were documented before, but I'll put something in the upgrade guide for 1.0 just to let folks know they're officially gone. Thanks for bringing this to our attention @G2Jose!
Most helpful comment
@G2Jose thanks for the detailed explanation! Digging in and will get back to you ASAP-- I agree on the blueprint routes themselves seeming unexpected here vs what's documented.
In the mean time, just to point this out in case it's helpful: Any time you're doing
GET /users/1/accounts/2, you can actually just shave off the front and be more concise-- i.e. just doGET /accounts/2(because2is a unique id across _all_ account records).