Sequelize One-to-One Example - Nodejs Association
Sequelize
13
nodejs
133
association
1
Male avatar

loveprogramming viết ngày 28/03/2021

https://loizenai.com/sequelize-one-to-one-example/

In the tutorial, I guide how to create Sequelize One-To-One example association models with NodeJS/Express, MySQL.

  • Technologies for Sequelize Nodejs Association One to One:
  • Nodejs
  • Express
  • Sequelize
  • MySQL

Sequelize One-to-One Example 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

Customer.belongsTo(Address); // Will add an attribute customerId to the Address model.

What is difference between belongsTo and hasOne?
-> hasOne inserts the association key in target model whereas BelongsTo inserts the association key in the source model.

We can create a solution by combine foreignKey and targetKey with belongsTo and hasOne relation as below code:


Customer = sequelize.define('customer', {
  uuid: {
    type: Sequelize.UUID,
    defaultValue: Sequelize.UUIDV1,
    primaryKey: true
  },
  /*
    more attributes
  */
});

Address = sequelize.define('address', {
    /* attributes */
});
    
Address.belongsTo(Customers, {foreignKey: 'fk_customerid', targetKey: 'uuid'});
Customers.hasOne(Address, {foreignKey: 'fk_customerid', targetKey: 'uuid'});

Foreign Key

  • In belongsTo relation, foreign key will be generated from the target model name and the target primary key name. Sequelize provides a foreignKey option to override defaultValue.

Target Key

  • The target key is the column on the target model that the foreign key column on the source model points to. In belongsTo relation, by default the target key will be the target model's primary key. Sequelize provides a targetKey option to define a custom column.

How to save it?


var customer;
Customer.create({ 
    firstname: 'Jack',
    ...
    }).then(createdCustomer => {        
        // Send created customer to client
        customer = createdCustomer;
        
        return Address.create({
            street: 'W NORMA ST',
            ...
        })
    }).then(address => {
        customer.setAddress(address)
    })
};

How to fetch entities? - Tutorial: "Sequelize One-to-One Example Association - Nodejs MySQL"

More: https://loizenai.com/sequelize-one-to-one-example/

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

Male avatar

loveprogramming

447 bài viết.
78 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
Male avatar
1 0
Tutorial Link: (Link) (Ảnh) Django is a Pythonbased free and opensource web framework that follows the modeltemplateview architectural pattern. A...
loveprogramming viết 6 tháng trước
1 0
Male avatar
1 0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...
loveprogramming viết 5 tháng trước
1 0
Male avatar
1 0
Angular Spring Boot jwt Authentication Example Github https://loizenai.com/angularspringbootjwt/ (Ảnh) Tutorial: ” Angular Spring Boot jwt Authe...
loveprogramming viết 5 tháng trước
1 0
Bài viết liên quan
Male avatar
1 0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...
loveprogramming viết 5 tháng trước
1 0
Male avatar
0 0
https://grokonez.com/frontend/angular/angular6/angular6httpinterceptorwithnodejsrestapis Angular provides HTTP Interception to inspect and transfo...
loveprogramming viết 2 tháng trước
0 0
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
447 bài viết.
78 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!