This article describes how to make foreign keys non-null when the association is set by using the belongsTo method etc. in Sequelize of Node.js. The related resources are shown below.
The workflow is shown below.
- Creating a database
- Coding preparation
- Coding
- Operation check
Execute the following SQL statements to create a database for operation check.
create database tech_blog charset utf8;
create user tech_blog@localhost identified by'password';
grant all privileges on tech_blog.* to tech_blog@localhost;
To execute the SQL statements from the terminal, run the following command (if you have not set a password, the -p option is not required).
mysql -u root -p
Run the following commands in the terminal to prepare for coding.
mkdir sequelize-foreign-key
cd sequelize-foreign-key
npm init -y
npm install --save dotenv mysql2 sequelize
touch .env main.js
Open main.js in the editor and enter the following content.
The point is shown below.
- Set the foreignKey option (second argument) of the belongsTo method to
{allowNull: false}
.
Open .env in the editor and enter the following point.
- DB_URL: Database connection string (e.g. mysql://tech_blog:password@localhost:3306/tech_blog)
Run the following command in the terminal to create the table.
node -r dotenv/config main.js
Check that the following two points are included in the execution result.
userId
INTEGER NOT NULLarticleId
INTEGER NOT NULL
The execution result is shown below.
Executing (default): DROP TABLE IF EXISTS `userArticle`;
Executing (default): DROP TABLE IF EXISTS `article`;
Executing (default): DROP TABLE IF EXISTS `user`;
Executing (default): DROP TABLE IF EXISTS `user`;
Executing (default): CREATE TABLE IF NOT EXISTS `user` (`id` INTEGER NOT NULL auto_increment, `name` VARCHAR (255) NOT NULL,` createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (` id`)) ENGINE = InnoDB;
Executing (default): SHOW INDEX FROM `user`
Executing (default): DROP TABLE IF EXISTS `article`;
Executing (default): CREATE TABLE IF NOT EXISTS `article` (`id` INTEGER NOT NULL auto_increment, `title` VARCHAR (255) NOT NULL,` createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (` id`)) ENGINE = InnoDB;
Executing (default): SHOW INDEX FROM `article`
Executing (default): DROP TABLE IF EXISTS `userArticle`;
Executing (default): CREATE TABLE IF NOT EXISTS `userArticle` (`id` INTEGER NOT NULL auto_increment, `createdAt` DATETIME NOT NULL,` updatedAt` DATETIME NOT NULL, `userId` INTEGER NOT NULL,` articleId` INTEGER NOT NULL, PRIMARY KEY (`id`), FOREIGN KEY (`userId`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY (`articleId`) REFERENCES` article` (` id`) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE = InnoDB;
Executing (default): SHOW INDEX FROM `userArticle`
I remember struggling to find a way to make foreign keys non-null from the official Sequelize documentation in the past, but now it's easy to find from the Associations page. In that case, the significance of the existence of this article will be lost, but since I wrote it with great effort, I post it because a bad bush is better than the open field. Thank you for reading!