Fix(crud) include param lookup now works w/plurals

Previously, {one,many}-to-many relationships with models would result in
`associationNames` that were plural. e.g. `Team` might have many
players and one location. The validation was expecting to see the plural
`Players` and the singular `Location` but Sequelize is expecting the
singular `Player` (`Location` worked fine). This meant that include
lookups would silently fail. This fixes the problem in a backward-
compatible way.

It continues to allow `include=Location` (capitalized) for backward-
compatibility. And now allows and actually does the lookup for
`include=players`, `include=player`, `include=Player`, `include=Players`
lookup relationships.
This commit is contained in:
Joey Baker
2016-10-26 11:19:36 -07:00
parent 07176018b7
commit bcb7861061
4 changed files with 29 additions and 3 deletions

View File

@ -109,7 +109,7 @@ Getting related models is easy, just use a query parameter `include`.
```js
// returns all teams with their related City model
// GET /teams?include=City
// GET /teams?include=city
// results in a Sequelize query:
Team.findAll({include: City})
@ -118,12 +118,21 @@ Team.findAll({include: City})
If you want to get multiple related models, just pass multiple `include` parameters.
```js
// returns all teams with their related City and Uniform models
// GET /teams?include=City&include=Uniform
// GET /teams?include[]=city&include[]=uniform
// results in a Sequelize query:
Team.findAll({include: [City, Uniform]})
```
For models that have a many-to-many relationship, you can also pass the plural version of the association.
```js
// returns all teams with their related City and Uniform models
// GET /teams?include=players
// results in a Sequelize query:
Team.findAll({include: [Player]})
```
## `limit` and `offset` queries
Restricting list (`GET`) and scope queries to a restricted count can be done by passing `limit=<number>` and/or `offset=<number>`.