- Node.js 6.x Blueprints
- Fernando Monteiro
- 258字
- 2021-07-14 10:35:06
Creating Band schema
Let's create the schema that will store in the database the data of each band that the user creates in the system.
- Open terminal/shell and type the following command:
sequelize model:create --name Band --attributes "name:string, description:string, album:string, year:string, UserId:integer"
- As in the previous step, two files were created, one for migration of data and another to be used as a Band model, as the following code:
'use strict'; module.exports = function(sequelize, DataTypes) { var Band = sequelize.define('Band', { name: DataTypes.STRING, description: DataTypes.STRING, album: DataTypes.STRING, year: DataTypes.STRING, UserId: DataTypes.INTEGER }, { classMethods: { associate: function(models) { // associations can be defined here } } }); return Band; };
Creating associations between Band and User models
As the last step before using the schemes migration script, we will need to create the associations between the User model and the Band model. We will use the following associations:

Tip
You can find more about associations at the following link: http://docs.sequelizejs.com/en/latest/docs/associations/.
- Open the
User.js
model and add the following highlighted code:'use strict'; module.exports = function(sequelize, DataTypes) { var User = sequelize.define('User', { name: DataTypes.STRING, email: DataTypes.STRING }, { classMethods: { associate: function(models) { // associations can be defined here User.hasMany(models.Band); } } }); return User; };
- Open the
Band.js
model and add the following highlighted code:'use strict'; module.exports = function(sequelize, DataTypes) { var Band = sequelize.define('Band', { name: DataTypes.STRING, description: DataTypes.STRING, album: DataTypes.STRING, year: DataTypes.STRING, UserId: DataTypes.INTEGER }, { classMethods: { associate: function(models) { // associations can be defined here Band.belongsTo(models.User); } } }); return Band; };
推薦閱讀
- Building a Game with Unity and Blender
- Production Ready OpenStack:Recipes for Successful Environments
- Rust Cookbook
- 編譯系統(tǒng)透視:圖解編譯原理
- 零基礎(chǔ)學(xué)Python網(wǎng)絡(luò)爬蟲案例實(shí)戰(zhàn)全流程詳解(高級進(jìn)階篇)
- 程序是怎樣跑起來的(第3版)
- C語言程序設(shè)計(jì)教程
- 常用工具軟件立體化教程(微課版)
- 零基礎(chǔ)看圖學(xué)ScratchJr:少兒趣味編程(全彩大字版)
- Python趣味創(chuàng)意編程
- Spring Boot從入門到實(shí)戰(zhàn)
- Mastering Magento Theme Design
- Learning QGIS(Second Edition)
- Learning Yeoman
- Natural Language Processing with Java Cookbook