sequelize / sequelize

Feature-rich ORM for modern Node.js and TypeScript, it supports PostgreSQL (with JSON and JSONB support), MySQL, MariaDB, SQLite, MS SQL Server, Snowflake, Oracle DB (v6), DB2 and DB2 for IBM i.
https://sequelize.org/
MIT License
29.42k stars 4.26k forks source link

find with association issue #7615

Closed ylarom closed 7 years ago

ylarom commented 7 years ago

I have a social network like app, where each post has a userId referring to the user

var User = sequelize.define('User', { ... }) User.hasMany(models.Post, { as: 'posts', foreignKey: 'userId', onDelete: 'cascade' });

I'm fetching a user with their posts

According to the documentation at http://docs.sequelizejs.com/en/latest/docs/models-usage/ I tried: User.find( { where: { id: userId }, include: [ { association: 'posts' } ] } )

But that results with an error: Cannot read property 'name' of undefined at conformInclude (.../node_modules/sequelize/lib/model.js:291:45)

Looking at the code it seems conformIncludes expects an Association object and not the association name as string

The way it actually works is:

User.find( { where: { id: userId }, include: [ { association: User.associations.posts } ] })

Was I doing it wrong or should the documentation be changed?

sushantdhiman commented 7 years ago

I didnt find any posts reference may be because we updated docs. But you should prefer using association object