Master relationships between tables, complex queries, and transactions to build real-world databases.
One-to-One
One-to-Many
Many-to-Many
Operators
Transactions
Quiz
One-to-One Relationships
Think of a family tree. Relationships in a database work just like family relationships. A one-to-one relationship is like a person and their passport — each person has exactly one passport, and each passport belongs to exactly one person.
In Sequelize, you define one-to-one relationships using hasOne and belongsTo:
JavaScript — One-to-One: User & Profile
One-to-Many Relationships
A one-to-many relationship is like a parent and children. One parent can have many children, but each child has exactly one biological parent. In databases: one user can write many blog posts, but each post has only one author.
JavaScript — One-to-Many: User & Posts
Many-to-Many Relationships
A many-to-many relationship is like students and courses. One student can enroll in many courses, and one course can have many students. To track this, you need a junction table (also called a "join table") that connects the two.
JavaScript — Many-to-Many: Posts & Tags
Relationship Summary
Relationship
Example
Sequelize Methods
One-to-One
User ↔ Profile
hasOne / belongsTo
One-to-Many
User ↔ Posts
hasMany / belongsTo
Many-to-Many
Posts ↔ Tags
belongsToMany (with through table)
Sequelize Operators
Operators let you build complex queries — filtering by ranges, patterns, lists, and more.
JavaScript — Sequelize Operators
Operator Reference
Operator
SQL Equivalent
Example
Op.eq
=
Exact match
Op.ne
!=
Not equal
Op.gt / Op.gte
> / >=
Greater than
Op.lt / Op.lte
< / <=
Less than
Op.between
BETWEEN
Range of values
Op.like
LIKE
Pattern matching
Op.in
IN
Match any in list
Op.and / Op.or
AND / OR
Combine conditions
Transactions
A transaction is like a bank transfer. When you transfer $100 from Account A to Account B, two things must happen: A loses $100 AND B gains $100. If the second step fails, the first must be reversed. It is all or nothing — you never want money to disappear into thin air! Database transactions work the same way.
JavaScript — Sequelize Transactions
Always use transactions when multiple database operations must succeed or fail together. Without a transaction, a crash between operations could leave your data in an inconsistent state.
📝 Chapter 16 Quiz
1. Which Sequelize methods define a one-to-one relationship?
hasMany + belongsTo
belongsToMany + through
hasOne + belongsTo
connectOne + linkTo
2. What is needed for a many-to-many relationship?
A foreign key on both tables
A junction table (through table)
A special many-to-many column type
Two hasMany declarations
3. How do you fetch a user WITH their posts in one query?
Use include: Post in findOne/findAll
Use join: Post in the where clause
Make two separate queries and merge them
Use User.withPosts()
4. Which operator finds posts with "Node" in the title?
Op.eq with 'Node'
Op.in with ['Node']
Op.contains with 'Node'
Op.like with '%Node%'
5. What happens if an error occurs inside a managed transaction?
Only the failed operation is skipped
All operations inside the transaction are rolled back