- 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; };
推薦閱讀
- 多媒體CAI課件設(shè)計(jì)與制作導(dǎo)論(第二版)
- Pandas Cookbook
- 深入理解Django:框架內(nèi)幕與實(shí)現(xiàn)原理
- C++面向?qū)ο蟪绦蛟O(shè)計(jì)(微課版)
- Visual FoxPro 程序設(shè)計(jì)
- FreeSWITCH 1.6 Cookbook
- Dependency Injection in .NET Core 2.0
- Oracle 12c中文版數(shù)據(jù)庫(kù)管理、應(yīng)用與開(kāi)發(fā)實(shí)踐教程 (清華電腦學(xué)堂)
- Scratch 3.0少兒編程與邏輯思維訓(xùn)練
- Python漫游數(shù)學(xué)王國(guó):高等數(shù)學(xué)、線性代數(shù)、數(shù)理統(tǒng)計(jì)及運(yùn)籌學(xué)
- Learning Hunk
- Android傳感器開(kāi)發(fā)與智能設(shè)備案例實(shí)戰(zhàn)
- 遠(yuǎn)方:兩位持續(xù)創(chuàng)業(yè)者的點(diǎn)滴思考
- INSTANT Premium Drupal Themes
- C Primer Plus(第6版)中文版【最新修訂版】