On this page
数据表设计和迁移
创建数据迁移表
shell
npx sequelize migration:generate --name=video
npx sequelize migration:generate --name=video_detail
npx sequelize migration:generate --name=video_play
1.执行完命令后,会在database / migrations / 目录下生成数据表迁移文件,然后定义
js
// video
"use strict";
module.exports = {
up: (queryInterface, Sequelize) => {
const { INTEGER, STRING, DATE, ENUM, TEXT } = Sequelize;
return queryInterface.createTable("video", {
id: {
type: INTEGER(20),
primaryKey: true,
autoIncrement: true,
},
title: {
type: STRING(100),
allowNull: false,
defaultValue: "",
comment: "视频标题",
},
cover: {
type: STRING,
allowNull: true,
defaultValue: "",
comment: "视频封面",
},
category_id: {
type: INTEGER,
allowNull: false,
defaultValue: 0,
comment: "分类id",
references: {
model: "category",
key: "id",
},
onDelete: "cascade",
onUpdate: "restrict", // 更新时操作
},
user_id: {
type: INTEGER,
allowNull: false,
defaultValue: 0,
comment: "用户id",
references: {
model: "user",
key: "id",
},
onDelete: "cascade",
onUpdate: "restrict", // 更新时操作
},
duration: {
type: INTEGER,
allowNull: false,
defaultValue: 0,
comment: "总时长",
},
desc: {
type: TEXT,
allowNull: false,
defaultValue: "",
comment: "视频描述",
},
play_count: {
type: INTEGER,
allowNull: false,
defaultValue: 0,
comment: "播放量",
},
danmu_count: {
type: INTEGER,
allowNull: false,
defaultValue: 0,
comment: "弹幕量",
},
created_time: DATE,
updated_time: DATE,
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable("video");
},
};
js
// video_detail
"use strict";
module.exports = {
up: (queryInterface, Sequelize) => {
const { INTEGER, STRING, DATE, ENUM, TEXT } = Sequelize;
return queryInterface.createTable("video_detail", {
id: {
type: INTEGER(20),
primaryKey: true,
autoIncrement: true,
},
title: {
type: STRING(100),
allowNull: false,
defaultValue: "",
comment: "子标题",
},
video_id: {
type: INTEGER,
allowNull: false,
defaultValue: 0,
comment: "视频id",
references: {
model: "video",
key: "id",
},
onDelete: "cascade",
onUpdate: "restrict", // 更新时操作
},
url: {
type: STRING,
allowNull: true,
defaultValue: "",
comment: "视频链接",
},
desc: {
type: TEXT,
allowNull: false,
defaultValue: "",
comment: "子描述",
},
created_time: DATE,
updated_time: DATE,
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable("video_detail");
},
};
js
// video_play
"use strict";
module.exports = {
up: (queryInterface, Sequelize) => {
const { INTEGER, STRING, DATE, ENUM, TEXT } = Sequelize;
return queryInterface.createTable("video_play", {
id: {
type: INTEGER(20),
primaryKey: true,
autoIncrement: true,
},
ip: {
type: STRING(50),
allowNull: false,
defaultValue: 0,
comment: "ip地址",
},
video_id: {
type: INTEGER,
allowNull: false,
defaultValue: 0,
comment: "视频id",
references: {
model: "video",
key: "id",
},
onDelete: "cascade",
onUpdate: "restrict", // 更新时操作
},
created_time: DATE,
updated_time: DATE,
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable("video_play");
},
};
执行 migrate 进行数据库变更 npx sequelize db:migrate 模型创建
js
// app/model/video.js
module.exports = (app) => {
const { STRING, INTEGER, DATE, ENUM, TEXT } = app.Sequelize;
const Video = app.model.define("video", {
id: {
type: INTEGER(20),
primaryKey: true,
autoIncrement: true,
},
title: {
type: STRING(100),
allowNull: false,
defaultValue: "",
comment: "视频标题",
},
cover: {
type: STRING,
allowNull: true,
defaultValue: "",
comment: "视频封面",
},
category_id: {
type: INTEGER,
allowNull: false,
defaultValue: 0,
comment: "分类id",
},
user_id: {
type: INTEGER,
allowNull: false,
defaultValue: 0,
comment: "用户id",
},
duration: {
type: INTEGER,
allowNull: false,
defaultValue: 0,
comment: "总时长",
},
desc: {
type: TEXT,
allowNull: false,
defaultValue: "",
comment: "视频描述",
},
created_time: {
type: DATE,
get() {
return (new Date(this.getDataValue("created_time"))).getTime();
},
},
play_count: {
type: INTEGER,
allowNull: false,
defaultValue: 0,
comment: "播放量",
},
danmu_count: {
type: INTEGER,
allowNull: false,
defaultValue: 0,
comment: "弹幕量",
},
updated_time: DATE,
});
// 关联关系
Video.associate = function (models) {
// 关联作者
Video.belongsTo(app.model.User);
// 关联子视频
Video.hasMany(app.model.VideoDetail);
};
return Video;
};
js
// app/model/video_detail.js
module.exports = (app) => {
const { STRING, INTEGER, DATE, ENUM, TEXT } = app.Sequelize;
const VideoDetail = app.model.define("video_detail", {
id: {
type: INTEGER(20),
primaryKey: true,
autoIncrement: true,
},
title: {
type: STRING(100),
allowNull: false,
defaultValue: "",
comment: "子标题",
},
video_id: {
type: INTEGER,
allowNull: false,
defaultValue: 0,
comment: "视频id",
},
url: {
type: STRING,
allowNull: true,
defaultValue: "",
comment: "视频链接",
},
desc: {
type: TEXT,
allowNull: false,
defaultValue: "",
comment: "子描述",
},
created_time: DATE,
updated_time: DATE,
});
// 关联关系
VideoDetail.associate = function (models) {
// 关联视频
VideoDetail.belongsTo(app.model.Video);
};
return VideoDetail;
};
js
// app/model/video_play.js
module.exports = (app) => {
const { STRING, INTEGER, DATE, ENUM, TEXT } = app.Sequelize;
const VideoPlay = app.model.define("video_play", {
id: {
type: INTEGER(20),
primaryKey: true,
autoIncrement: true,
},
ip: {
type: STRING(50),
allowNull: false,
defaultValue: 0,
comment: "ip地址",
},
video_id: {
type: INTEGER,
allowNull: false,
defaultValue: 0,
comment: "视频id",
},
created_time: DATE,
updated_time: DATE,
});
return VideoPlay;
};