Sequelize One-to-One Example – Nodejs Association
Tags: sequelize, node, onetoone
Published: 2021-03-28 16:05:18

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

<!--more-->
<h2 id="sequelize-one-to-one-example-association">Sequelize One-to-One Example Association</h2>
<strong>Note</strong>: When calling a method such as <code>Customer.hasOne(Address)</code>, we say that the Customer model is the source and the Address model is the target.

We define 2 models:

<pre><code class="language-javascript">
Customer = sequelize.define('customer', {
  /* attributes */
});

Address = sequelize.define('address', {
	/* attributes */
});
</code></pre>

How to create One-to-One association between them?
-> Sequelize provides 2 ways:

- <strong>belongsTo</strong>

<pre><code class="language-javascript">
Address.belongsTo(Customer); // Will add a customerId attribute to Address to hold the primary key value for Customer.
</code></pre>

- <strong>hasOne</strong>

<pre><code class="language-javascript">
Customer.belongsTo(Address); // Will add an attribute customerId to the Address model.
</code></pre>

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

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

<pre><code class="language-javascript">
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'});
</code></pre>

<strong>Foreign Key</strong>
 
- In <strong>belongsTo</strong> relation, foreign key will be generated from the target model name and the target primary key name. Sequelize provides a <code>foreignKey</code> option to override defaultValue.

<strong>Target Key</strong>

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

<strong>How to save it</strong>?

<pre><code class="language-javascript">
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)
	})
};</code></pre>

<strong>How to fetch entities</strong>? - Tutorial: "Sequelize One-to-One Example Association - Nodejs MySQL"

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