Bạn có chắc chắn muốn xóa bài viết này không ?
Bạn có chắc chắn muốn xóa bình luận này không ?
Sequelize One-To-One association – NodeJS/Express, MySQL
https://grokonez.com/node-js/sequelize-one-to-one-association-nodejs-express-mysql
Sequelize One-To-One association – NodeJS/Express, MySQL
In the post, we got started with Sequelize ORM. Today we're gonna create Sequelize One-To-One association models with NodeJS/Express, MySQL.
Related posts:
- Sequelize ORM – Build CRUD RestAPIs with NodeJs/Express, Sequelize, MySQL
- Sequelize One-To-Many association – NodeJS/Express, MySQL
- Sequelize Many-to-Many association – NodeJS/Express, MySQL
Sequelize One-to-One association
Note: When calling a method such as Customer.hasOne(Address)
, we say that the Customer model is the source and the Address model is the target.
We define 2 models:
Customer = sequelize.define('customer', {
/* attributes */
});
Address = sequelize.define('address', {
/* attributes */
});
How to create One-to-One association between them?
-> Sequelize provides 2 ways:
- belongsTo
Address.belongsTo(Customer); // Will add a customerId attribute to Address to hold the primary key value for Customer.
- hasOne
More at:
https://grokonez.com/node-js/sequelize-one-to-one-association-nodejs-express-mysql
Sequelize One-To-One association – NodeJS/Express, MySQL





