paint-brush
Sequelize: Association Methods Make Life Easierby@sarahscode
134 reads

Sequelize: Association Methods Make Life Easier

by Sarah KatzNovember 5th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

I can’t count the number of times that the phrase “I don’t understand Sequelize associations” came out of my mouth this past week. I’m pretty good at creating tables and adding/modifying/deleting instances, but I’ve never really worked well with associations. Don’t get me wrong, I can write a “Order.hasMany(Products)” association with the best of them, but I never really felt any need for those getProducts() and addProducts() methods.

Company Mentioned

Mention Thumbnail
featured image - Sequelize: Association Methods Make Life Easier
Sarah Katz HackerNoon profile picture

I can’t count the number of times that the phrase “I don’t understand Sequelize associations” came out of my mouth this past week. I’m pretty good at creating tables and adding/modifying/deleting instances, but I’ve never really worked well with associations. Don’t get me wrong, I can write a “Order.hasMany(Products)” association with the best of them, but I never really felt any need for those getProducts() and addProducts() methods.

Until this week, that is.

Senior phase of my bootcamp at Fullstack means projects, and right now we’re working on a group project building a simple e-commerce site. We were able to quickly associate our orders and products (what we’re selling is common enough that we know it will be a many-to-many association), and we even built a join table to associate the two, but when it came to querying the database, we ran into some trouble.

We tried directly writing rows in our join table (which we called order_product), but that just caused a lot of trouble. If we later wanted to update that row (which we did — we wanted to set the price when the order was finalized), we’d have to query the product database to get the price and then update the order_product database.

Our whole group was working on this issue and making suggestions, and while we had some great ideas, nothing seemed to work as we wanted. I tried some quick Google searches, and eventually wound up in the section of the Sequelize documentation that discusses associations and the methods attached to many-to-many associations.

Suddenly … our problems were solved (okay, maybe it wasn’t that easy). We decided to try the addProduct() association method, and once we got the syntax right, our order_product table was creating its own rows without us having to query it directly. And when we needed to find the current product price, rather than having to query the order_product table to get all the products and then the product table to get the prices, we were able to just use the getProducts() method to get an array of all the products in our order (although in that case we still did have to query the order_product table to update the price). Instead of querying the order_product table to get each product in the array, then querying the product table to get the price, then updating the order_product table, we were just using one quick association method to get all of the products (no need to query the products table!) and then running one query to update the price. Much easier than even attempting to do this without the association methods.

One note: we used ‘addProduct()’ to attach each individual product as part of the order — that was not something we were able to do in bulk (we tried, but that’s not how the addProduct() method seems to work). But we were able to retrieve all of the products at once using getProducts(). Just something to remember when using these methods.

I still wouldn’t say that I’m an expert at Sequelize associations. But I definitely had an “ah-ha!” moment this week, and I am starting to understand them better. Through the Sequelize issues my team experienced this week, I learned that it never hurts to try something new. I am confident now that I can use associations methods when working with Sequelize, and I’d definitely encourage others to try it (even beginners) — once you get the hang of it, these methods really are fantastic time savers!