✒️
SEI 802
  • Introduction
  • About These Notes
  • Syllabus
  • Development Workflow
    • Installfest
      • Mac OSX
      • Linux
      • Git Configuration
      • Sublime Packages
    • Command Line
      • The Terminal
      • Filesystem Navigation
      • File Manipulation
      • Additional Topics
    • Intro to Git
      • Version Control
      • Local Git
      • Remote Git
      • Git Recipes
    • Group Collaboration
      • Git Workflows
      • Project Roles and Tools
    • VS Code Tips & Tricks
  • HTML/CSS
    • HTML
    • CSS Selectors
    • CSS Box Model and Positioning
      • Box Model
      • Display and Positioning
      • Flexbox
      • Grid
      • Flexbox & Grid Games
      • Floats and Clears
      • Additional Topics
    • Advanced CSS
      • Responsive Design
      • Pseudo-Classes/Elements
      • Vendor Prefixes
      • Custom Properties
      • Additional Topics
    • Bootstrap
    • CSS Frameworks
    • Accessibility
  • JavaScript
    • Primitives
    • Arrays
    • Objects
    • Control Flow
      • Boolean Expressions
      • Conditionals
      • Loops
      • Promises
    • Functions
      • Callbacks
      • Timing Functions
      • Iterators
    • DOM and Events
    • DOM Manipulation
    • HTML5 Canvas
    • How To Reduce Redundancy
    • (2019) JavaScript OOP
    • (2016) OOP with Classes
    • (1995) OOP with Prototypes
      • Constructors
      • Prototypes
    • Intro to TDD
    • Scoping
    • Inheritance
      • Prototypal Inheritance
      • Call, Apply, and other Functions
      • ES6 Inheritance
      • Resources
    • Custom Node Modules
    • Additional Topics
      • AJAX, Fetch, and Async/Await
      • AJAX w/JSON and Localstorage
        • AJAX w/JSON
        • Local Storage
      • Async module
      • Data Scraping
  • jQuery
    • Intro
      • DOM Manipulation
      • Reddit Practice
      • Styling
      • Events
    • Plugins
    • AJAX
  • APIs
    • Fetch
    • AJAX w/jQuery
    • AJAX w/Fetch
  • Databases
    • Intro to SQL
    • Advanced SQL
    • MongoDB
      • Intro to NoSQL
      • CRUD in MongoDB
      • Data Modeling
      • Intermediate Mongo
  • Node/Express
    • Node
      • Intro to Node
      • Node Modules
      • Node Package Manager (NPM)
    • Express
      • Intro to Express
        • Routes
        • Views
        • Templates
        • Layouts and Controllers
        • CRUD & REST
          • Get and Post
          • Put and Delete
      • APIs with Express (request)
      • APIs with Express (axios)
    • Sequelize
      • Terminology
      • Setup
      • Using Models
      • Seeding Data
      • Validations and Migrations
      • Resources
      • 1:M Relationships
      • N:M Relationships
    • Express Authentication
      • Research Components
      • Code Components
      • Auth in Theory
        • Sessions
        • Passwords
        • Middleware
        • Hooks
      • Auth in Practice
        • Create the User
        • User Signup
        • Sessions
        • User Login
        • Authorization and Flash messages
    • Testing with Mocha and Chai
    • Mongoose
      • Mongoose Associations
    • JSON Web Tokens
      • Codealong
    • Additional Topics
      • oAuth
      • Geocoding with Mapbox
      • Geocoding and Google Maps
      • Cloudinary
      • Websockets with Socket.io
      • SASS
  • Ruby
    • Intro to Ruby
    • Ruby Exercises
    • Ruby Classes
    • Ruby Testing with Rspec
    • Ruby Inheritance
    • Ruby Data Scraping
  • Ruby on Rails
    • Intro to Rails
    • APIs with Rails
    • Asset Pipeline
    • Rails Auth and 1-M
      • Auth Components
    • Rails N:M
    • ActiveRecord Polymorphism
    • Additional Topics
      • oAuth
      • SASS
      • Rails Mailers
      • Cloudinary
      • Jekyll
  • React (Updated 2019)
    • ES6+/ESNext
      • Const and Let
      • Arrow Functions
      • Object Literals and String Interpolation
      • ES6 Recap
      • ES6 Activity
    • Intro to React
      • Create React App
      • Components and JSX
      • Virtual DOM
      • Props
      • Dino Blog Activity
      • Nested Components
      • Lab: LotR
    • React State
      • Code-Along: Mood Points
      • Code-Along: Edit Dino Blog
      • Lab: Simple Calc
      • Lifting State
    • React Router
      • Browser History/SPAs
      • React Router (lesson and full codealong)
      • Router Lab
    • Fetch and APIs
      • APIs with Fetch and Axios
      • Fetch the Weather
    • React Hooks
    • React LifeCycle
      • Lab: Component LifeCycle
    • React Deployment
    • Additional Topics
      • React Frameworks
        • Material UI Theming
      • Typescript
        • More Types and Syntax
        • Tsconfig and Declaration Files
        • Generics with Linked List
      • Redux
      • TypeScript
      • Context API
      • React Native
  • Meteor
  • Deployment and Config
    • Deploy - Github Pages
    • Deploy - Node/Sequelize
    • Deploy - Node/MongoDB
    • Deploy React
    • Deploy - Rails
      • Foreman (Environment Variables)
    • Deploy - AWS Elastic Beanstalk
    • Deploy - S3 Static Sites
    • Deploy - Django
    • Deploy - Flask
  • Data Structures and Algorithms
    • Recursion
    • Problem Solving - Array Flatten
    • Binary Search
    • Algorithm Complexity
    • Stacks and Queues
    • Bracket Matching
    • Ruby Linked Lists
      • Sample Code
      • Beginner Exercises
      • Advanced Exercises
    • JS Linked Lists
      • Sample Code
      • Beginner Exercises
      • Beginner Solutions
    • Hash Tables
    • Intro to Sorting
    • Insertion Sort
    • Bucket Sort
    • Bubble Sort
    • Merge Sort
    • Quick Sort
    • Heap Sort
    • Sorting Wrapup
    • Hashmaps
    • Trees and Other Topics
  • Python
    • Python Installation
    • Intro to Python
    • Python Lists
    • Python Loops
    • Python Dictionaries
    • Python Sets and Tuples
    • Python Cheatsheet
    • Python Functions
    • Python Classes
    • Python Class Inheritance
    • Intro to Flask
    • Intro to SQLAlchemy
      • Flask and SQLAlchemy
    • Using PyMongo
    • Intro to Django
    • CatCollector CodeAlong
      • URLs, Views, Templates
      • Models, Migrations
      • Model Form CRUD
      • One-to-Many Relations
      • Many-to-Many Relations
      • Django Auth
    • Django Cheatsheet
    • Django Auth
    • Django Polls App Tutorial
    • Django School Tool Tutorial
    • Django 1:M Relationships
    • Custom Admin Views
    • Data Structures and Algorithms
      • Recursion
      • Binary Search
      • Stacks and Queues
      • Linked Lists
      • Binary Trees
      • Bubble Sort
      • TensorFlow & Neural Networks
    • Adjacent Topics
      • Raspberry Pi
      • Scripting
  • Assorted Topics
    • History of Computer Science
    • Regular Expressions
    • Intro to WDI (Course Info)
    • Being Successful in WDI
    • Internet Fundamentals
      • Internet Lab
    • User Stories and Wireframing
      • Wireframing Exercise: Build an Idea
    • Post WDI
      • Learning Resources
      • Deliverables -> Portfolio
      • FAQ
  • Projects
    • Project 1
    • Project 2
    • Project 3
      • Project 3 Pitch Guidelines
    • Project 4
    • Past Projects
      • Project 1
      • Project 2
      • Project 3
      • Project 4
      • Portfolios
    • Post Project 2
    • MEAN Hackathon
      • Part 1: APIs
      • Part 2: Angular
    • Portfolio
  • Web Development Trends
  • Resources
    • APIs and Data
    • Tech Websites
    • PostgreSQL Cheat Sheet
    • Sequelize Cheat Sheet
    • Database Administration
  • Archived Section
    • (Archived) ReactJS
      • Intro to React
        • Todo List Codealong
        • Additional Topics
      • Deploy React
      • React with Gulp and Browserify
        • Setting up Gulp
        • Additional Gulp Tasks
      • React Router
        • OMDB Router
        • OMDB Search
        • Additional Resources
      • React Animations
        • CSS Animations
    • AngularJS
      • Intro to AngularJS
        • Components and SPA
        • Create an Angular App
      • Angular Directives and Filters
      • Angular Animation
      • Angular Bootstrap Directives
        • Bootstrap Modals
      • Angular $http
      • Angular Services
        • Service Recipes
        • ngResource
        • Star Wars Codealong
      • Angular Routing
      • Angular + Express
      • Angular Authentication
        • Additional Topics
      • Angular Components
      • Angular Custom Filters
      • Angular Custom Directives
Powered by GitBook
On this page
  • Objectives
  • To Join the Many
  • Sequelize models
  • Update your Associations
  • pet.js
  • toys.js
  • Examples
  • Add a unique toy to a pet.
  • Get all pets that use a toy
  • Get all the data!

Was this helpful?

  1. Node/Express
  2. Sequelize

N:M Relationships

Previous1:M RelationshipsNextExpress Authentication

Last updated 3 years ago

Was this helpful?

Objectives

  • Create a join table and utilize it in a many-to-many relationship

  • Use Sequelize's helper methods to add relationships between two different models.

To Join the Many

When creating a many to many relationship, we need to have some way of creating that relationship. With a 1:M relationship, the id of the one is attached to the many (pets.userId in the 1:M relationship in our userapp). What do we do when there is no 1? When everything is many, we have to have some place to put the corrosponding ids.

Enter join tables! These tables have a one to many relationship with each of the relevant tables.

An er diagram featuring a many to many relationship with students and classes and a join table called enrollments

Often, the naming convention is to have the join table have the names of both tables. For examples if you have products and orders, the join table will often be called products_orders.

Sequelize models

We will be expanding our data model in userapp to include toys for our pets.

Very Important Name - your models singular, but your join model will be plural.

sequelize model:create --name toy --attributes type:string,color:string

sequelize model:create --name petsToys --attributes petId:integer,toyId:integer

Update your Associations

In order to associate pets to toys in a many to many fashion, you will need to update the associations on the pets and toys.

pet.js

 pet.associate = function(models) {
   // associations can be defined here
   models.pet.belongsTo(models.user);
   models.pet.belongsToMany(models.toy, {through: "petsToys"})
 };

toys.js

toys.associate = function(models) {
   models.toy.belongsToMany(models.pet, {through: "petsToys"})
 };

Examples

Add a unique toy to a pet.

In order to add a unique toy to a pet, we must first try to find or create a pet, in order to make sure it is in fact unique.

Secondly, we must attach a toy to the pet, using some built in helpers.

Some ORM has capabilities to do a bulk create on an object associations, but that kind of logic is not built in Sequelize.

// First, get a reference to a pet.
db.pet.findOrCreate({
  where: {
    name: "Silly May",
    species: "Mini Aussie"
  }
}).then(function([pet, created]) {
  // Second, get a reference to a toy.
  db.toy.findOrCreate({
    where: {type: "stinky bear", color: "brown"}
  }).then(function([toy, created]) {
    // Finally, use the "addModel" method to attach one model to another model.
    pet.addToy(toy).then(function(relationInfo) {
      console.log(toy.type, "added to", pet.name);
    });
  });
});

Take some time to use these helper functions to add more toys and more pets!

Get all pets that use a toy

Sequelize generates helper functions that allow you to get related items. For instance, if you wanted to find all pets that used a given toy:

db.toy.findOne({
  where: {type: "ball"}
}).then(function(toy) {
  toy.getPets().then(function(pets) {
    console.log(pets.length, 'pet(s) love the', toy.color, toy.type)
  });
});

You can use the addModel() helper function to add a pet association on a toy if there are no pet associations yet.

db.toy.findOrCreate({
  where: {type: "ball", color: "green"}
}).then(function([toy, created]) {
  toy.getPets().then(function(pets) {
    // Check if their are any pets associated with this toy
    if (pets.length > 0) {
      pets.forEach(function(pet) {
        console.log(pet.name, 'loves their', toy.color, toy.type);
      });
    } else {
      // findOrCreate a Pet and add it to the toy
      db.pet.findOrCreate({
        where: {
          name: "Ruby Tuesday",
          species: "Toy Aussie"
        }
      }).then(function([pet, created]) {
        toy.addPet(pet).then(function(relationInfo){
          console.log(pet.name, 'has faved the', toy.color, toy.type, 'toy')
        })
      });
    } // end of if statement
  });
});

Because this is a Many to Many association, all the logic from before can be turned around to search for all the toys of a particular pet!

db.pet.findOne({
  where: {name: "Ruby Tuesday"}
}).then(function(pet) {
  pet.getToys().then(function(toys) {
    toys.forEach(function(toy) {
      console.log(pet.name, 'loves their', toy.color, toy.type);
    });
  });
});

NOTE: In the above code, if Ruby Tuesday doesn't have any toys, that forEach function will crash the nodemon server! Make sure you have error handling so your whole app doesn't shut down because one pet isn't materialistic!

Get all the data!

Since we have a 1:M relationship between users and pets as well as a N:M relationship between pets and toys, we can get all our info through the Pet model. One of the easier ways of doing this is through the include keyword:

db.pet.findOne({
  where: {
    name: "Silly May"
  },
  include: [db.user, db.toy]
}).then(function(pet) {
  pet.toys.forEach(function(toy) {
    console.log(pet.user.firstName + '\'s pet', pet.name, 'loves their', toy.color, toy.type)
  })
})

Or we can use a mix of include and helper functions to get all the toys of all the pets of a certain user!

db.user.findByPk(1, { include: [db.pet] })
.then(function(user) {
  user.pets.forEach(function(pet) {
    pet.getToys().then(function(toys) {
      toys.forEach(function(toy) {
        console.log(user.firstName + '\'s pet', pet.name, 'loves their', toy.color, toy.type)
      })
    })
  })
})

As you can see, there are MANY (to) MANY ways to get associated data when it is needed. It's also easy to see how easy it can be to get lost in nesting hell. One way to help keep things clean is to comment the end of each section. If we take the last block of code as an example:

db.user.findByPk(1, { include: [db.pet] })
.then(function(user) {
  user.pets.forEach(function(pet) {
    pet.getToys().then(function(toys) {
      toys.forEach(function(toy) {
        console.log(user.firstName + '\'s pet', pet.name, 'loves their', toy.color, toy.type)
      }) // toys.forEach end
    }) // getToys end
  }) // pets.forEach end
}) // user.findByPk end