Discover how simple connections between data models can save you hours of confusing code!
Why Associations (hasMany, belongsTo) in Express? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have two lists: one for authors and one for books. You want to connect each book to its author manually by searching through both lists every time you need to find related data.
Manually linking data like this is slow and confusing. You might forget to update both lists, causing errors. It's hard to keep track of which books belong to which authors, especially as data grows.
Associations like hasMany and belongsTo let you define clear relationships between data models. This way, you can easily fetch all books for an author or find the author of a book without messy manual code.
const authorBooks = books.filter(book => book.authorId === author.id);
Author.hasMany(Book); Book.belongsTo(Author); const authorBooks = await author.getBooks();
It enables simple, reliable access to related data, making your code cleaner and your app faster.
Think of a blog where each post belongs to a user. Using associations, you can quickly show all posts by a user or find who wrote a post without extra searching.
Manual data linking is slow and error-prone.
Associations define clear, automatic relationships between models.
This makes data fetching easier, cleaner, and more reliable.
Practice
hasMany association represent between two models?Solution
Step 1: Understand the meaning of hasMany
ThehasManyassociation means one record in a model can be linked to many records in another model.Step 2: Compare with other associations
belongsTomeans one record belongs to one other record, not many.Final Answer:
One model instance can have multiple related instances of another model. -> Option AQuick Check:
hasMany = one-to-many [OK]
- Confusing hasMany with belongsTo
- Thinking hasMany means no relation
- Assuming hasMany means shared tables
Comment that belongs to Post?Solution
Step 1: Identify the direction of belongsTo
The model that belongs to another callsbelongsToon itself with the other model as argument.Step 2: Apply to Comment and Post
Since Comment belongs to Post, the syntax isComment.belongsTo(Post);.Final Answer:
Comment.belongsTo(Post); -> Option AQuick Check:
belongsTo called on child model [OK]
- Reversing the models in belongsTo
- Using hasMany instead of belongsTo
- Calling belongsTo on the wrong model
Author.hasMany(Book); Book.belongsTo(Author);
What will
await author.getBooks() return if author is an Author instance?Solution
Step 1: Understand hasMany and belongsTo setup
Author has many Books, and each Book belongs to one Author, so Sequelize creates methods like getBooks on Author instances.Step 2: Check what getBooks returns
getBooks returns an array of all Book instances linked to that Author.Final Answer:
An array of Book instances related to that author. -> Option BQuick Check:
hasMany creates plural get method returning array [OK]
- Expecting a single instance instead of array
- Thinking getBooks is invalid method
- Ignoring belongsTo association
Order.hasMany(Item); Item.belongsTo(Order); // Later in code const order = await Order.findByPk(1); const items = await order.getItem();
What is the error and how to fix it?
Solution
Step 1: Identify the association methods
Order hasMany Item means Sequelize creates getItems() method on Order instances, not getItem().Step 2: Fix the method call
Changeorder.getItem()toorder.getItems()to correctly fetch related items.Final Answer:
Method getItem() is incorrect; should be getItems() because hasMany creates plural getter. -> Option CQuick Check:
hasMany creates plural get method [OK]
- Using singular get method for hasMany
- Confusing belongsTo direction
- Thinking findByPk is invalid
User and Profile. Each User has one Profile, and each Profile belongs to one User. Which Sequelize associations correctly represent this, and what methods will be available on User instances?Solution
Step 1: Identify one-to-one relationship
Each User has one Profile meanshasOneis used, nothasMany.Step 2: Set belongsTo on Profile
Profile belongs to User, soProfile.belongsTo(User);is correct.Step 3: Check available methods
User instances getgetProfile()method for the single related Profile.Final Answer:
User.hasOne(Profile); Profile.belongsTo(User); with user.getProfile() method. -> Option DQuick Check:
One-to-one uses hasOne + belongsTo [OK]
- Using hasMany for one-to-one
- Reversing belongsTo direction
- Expecting plural get method for one-to-one
