SQL Databases with Sequelize
Sequelize is an ORM for Node that maps JavaScript objects to rows in a SQL database, so you can define models and run create, read, update, and delete operations without writing raw SQL.
Learn SQL Databases with Sequelize in our free Node.js course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Node.js course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll define a model, perform CRUD with methods like create , findAll , update , and destroy , link tables with associations, learn why migrations beat sync() in production, and see how Sequelize compares with Mongoose.
What You'll Learn in This Lesson
1️⃣ Defining a Model
A model is a JavaScript description of a table: its name and its columns (with types and rules). You connect to the database once, then sequelize.define(...) creates the model. Each field gets a DataTypes type (STRING, INTEGER, …) and optional constraints like allowNull or unique .
2️⃣ CRUD Operations
Once you have a model, the four basic operations each map to a method. create inserts a row, findAll and findByPk read, save (or update ) writes changes, and destroy deletes. All return Promises, so you await them. Filters go in a where clause, with operators like Op.gt for "greater than".
To see CRUD with real output and no database to install, here's a Map-backed model exposing the same method names:
3️⃣ Associations, Migrations & vs Mongoose
Associations connect tables: User.hasMany(Post) and Post.belongsTo(User) create the foreign key and let you load related rows together. For schema changes, prefer migrations — versioned, reversible scripts run with the Sequelize CLI — over sync() , which can silently alter or drop columns. And remember the big picture: Sequelize is for SQL databases with JOINs and foreign keys, while Mongoose (the previous database lesson) is for MongoDB documents.
Your turn. The model below works once you fill in the two method names marked ___ . Follow the 👉 hints, then run it.
No blanks — just a brief and an outline. Add a where -style filter to findAll so you can fetch only the rows that match. Build it, run it, and compare with the example output.
📋 Quick Reference — Sequelize CRUD
Practice quiz
What is Sequelize?
- A web server
- An ORM that maps JS objects to SQL rows
- A caching layer
- A test runner
Answer: An ORM that maps JS objects to SQL rows. Sequelize is an Object-Relational Mapper that lets you work with rows as JS objects.
Which method defines a model (a table) in Sequelize?
- sequelize.create()
- sequelize.table()
- sequelize.define()
- sequelize.model()
Answer: sequelize.define(). sequelize.define('User', {...}) describes a table with typed fields and constraints.
Which Sequelize method inserts a new row?
- create
- findAll
- destroy
- save
Answer: create. Model.create({...}) inserts a row and returns the created instance.
Which method reads a single row by its primary key?
- findAll
- findOne
- create
- findByPk
Answer: findByPk. findByPk(id) selects the row matching the given primary key.
Because Sequelize methods return Promises, what must you do?
- await them inside an async function
- Call them twice
- Wrap them in JSON.stringify
- Run them synchronously
Answer: await them inside an async function. Every model method returns a Promise, so you await it or you log a pending Promise.
Which method removes a row?
- findAll
- destroy
- create
- update
Answer: destroy. row.destroy() (or Model.destroy with a where) deletes the row.
Where do you put filters like 'age greater than 30' in a query?
- In the model definition
- In the connection string
- In a where clause with operators like Op.gt
- In a migration
Answer: In a where clause with operators like Op.gt. Filters go in { where: { age: { [Op.gt]: 30 } } } using Sequelize operators.
For production schema changes, what is preferred over sync()?
- Editing tables by hand
- Recreating the database
- Disabling constraints
- Versioned migrations
Answer: Versioned migrations. Migrations are reversible, source-controlled scripts; sync({alter:true}) can destroy data.
Which association pair links a User to many Posts?
- User.hasMany(Post) and Post.belongsTo(User)
- User.embed(Post)
- User.populate(Post)
- User.join(Post)
Answer: User.hasMany(Post) and Post.belongsTo(User). hasMany/belongsTo create the foreign key relationship for SQL joins.
Sequelize is for SQL databases; which library targets MongoDB documents instead?
- Redis
- Mongoose
- Knex
- Prisma
Answer: Mongoose. Mongoose is the document ORM for MongoDB, whereas Sequelize is for relational SQL databases.