1:M Relationships
Objectives
utilize table relationships in a database
add a 1:M association using Sequelize
use helper methods to add and access a related model
understand the purpose of eager loading when accessing a model's associations
Today we're going to cover how to setup a one to many relationship, using more than one table. This will allow data in one table to be associated with data in another.
Getting Started
We're going to build off the userapp
we created in the intro to sequelize. A bit of a recap on how to start an express sequelize app: 1. Create a directory for your app 2. cd
into that directory 3. Create your express route entry point (often index.js
, app.js
, or server.js
) 4. Initialize npm with npm init -y
5. Install your dependencies npm i express pg sequelize
6. Create your Database (either using sequelize-cli or in your psql shell) 7. Initialize Sequelize with sequelize init
8. Edit your config.json
file
Creating A Model
There are a few things to remember when creating models. Models shall be lowercase and singular. Sequlize will automatically create plural tables when any migrations are run. Also id, createdAt, and updatedAt fields are given for you. When we created our user, we ran this command:
When creating a table that will reference another table, use the following format, parentId
, when adding the foreign key to the table. This format is necessary for some of built in methods of Sequelize.
Adding the Associations
The following lines need to be inserted into the author and post models respectively in the associate
function. The comment line is where you will insert it.
Insert into models/user.js, inside the associate
function
Insert into models/pet.js, inside the associate
function
Finally, let's run create all the necessary tables from our models by migrating the database.
Read more: Seqeulize docs - One to Many
Using the association
Once the association is set up, we can use the createModel
, getModels
, setModel
, and addModel
helper methods. "Model" in each of these is replaced with the model name you create.
Creating an associated item with createModel
createModel
We can use the createPet
method to create a new pet associated with a user. Remeber to use the .then
promise.
Loading associated items using getModels
getModels
We can manually get all pets of a user by calling .getPets()
on a user instance. Remember this query is asynchronous and takes time, so we have to use a .then()
promise too.
Other methods
setModel
and addModel
are used to associate an existing record. If you created a pet and later wanted to add an association to an user this is how you'd do it.
Using include
include
Sequeize supports "eager loading", meaning it can load all of the pets for us in advance if we know we need them. We let it know what we need by using include
.
Last updated