On this page
数据表设计和迁移
创建数据迁移表
shell
# 朋友圈消息表
npx sequelize migration:generate --name=moment
# 朋友圈时间轴表
npx sequelize migration:generate --name=moment_timeline
# 朋友圈点赞表
npx sequelize migration:generate --name=moment_like
# 朋友圈评论表
npx sequelize migration:generate --name=moment_comment
1.执行完命令后,会在database / migrations / 目录下生成数据表迁移文件,然后定义
js
// moment表
"use strict";
module.exports = {
up: async (queryInterface, Sequelize) => {
const { INTEGER, STRING, DATE, ENUM, TEXT } = Sequelize;
// 创建表
await queryInterface.createTable("moment", {
id: {
type: INTEGER(20).UNSIGNED,
primaryKey: true,
autoIncrement: true,
},
content: {
type: TEXT,
allowNull: false,
defaultValue: "",
comment: "朋友圈内容",
},
image: {
type: TEXT,
allowNull: false,
defaultValue: "",
comment: "朋友圈图片",
},
video: {
type: STRING,
allowNull: false,
defaultValue: "",
comment: "朋友圈视频",
},
location: {
type: STRING,
allowNull: false,
defaultValue: "",
comment: "位置",
},
remind: {
type: STRING,
allowNull: false,
defaultValue: "",
comment: "提醒谁看",
},
see: {
type: STRING,
allowNull: false,
defaultValue: "all",
comment: "谁可以看 all公开 none私密",
},
user_id: {
type: INTEGER(20).UNSIGNED,
allowNull: false,
comment: "用户id",
// 定义外键(重要)
references: {
model: "user", // 对应表名称(数据表名称)
key: "id", // 对应表的主键
},
onUpdate: "restrict", // 更新时操作
onDelete: "cascade", // 删除时操作
},
created_at: DATE,
updated_at: DATE,
});
},
down: async (queryInterface) => {
await queryInterface.dropTable("moment");
},
};
js
// moment_timeline表
"use strict";
module.exports = {
up: async (queryInterface, Sequelize) => {
const { INTEGER, STRING, DATE, ENUM, TEXT } = Sequelize;
// 创建表
await queryInterface.createTable("moment_timeline", {
id: {
type: INTEGER(20).UNSIGNED,
primaryKey: true,
autoIncrement: true,
},
user_id: {
type: INTEGER(20).UNSIGNED,
allowNull: false,
comment: "用户id",
// 定义外键(重要)
references: {
model: "user", // 对应表名称(数据表名称)
key: "id", // 对应表的主键
},
onUpdate: "restrict", // 更新时操作
onDelete: "cascade", // 删除时操作
},
moment_id: {
type: INTEGER(20).UNSIGNED,
allowNull: false,
comment: "朋友圈消息id",
// 定义外键(重要)
references: {
model: "moment", // 对应表名称(数据表名称)
key: "id", // 对应表的主键
},
onUpdate: "restrict", // 更新时操作
onDelete: "cascade", // 删除时操作
},
own: {
type: INTEGER(1),
allowNull: false,
defaultValue: 0,
comment: "是否是自己的 0否1是",
},
created_at: DATE,
updated_at: DATE,
});
},
down: async (queryInterface) => {
await queryInterface.dropTable("moment_timeline");
},
};
js
// moment_like表
"use strict";
module.exports = {
up: async (queryInterface, Sequelize) => {
const { INTEGER, STRING, DATE, ENUM, TEXT } = Sequelize;
// 创建表
await queryInterface.createTable("moment_like", {
id: {
type: INTEGER(20).UNSIGNED,
primaryKey: true,
autoIncrement: true,
},
user_id: {
type: INTEGER(20).UNSIGNED,
allowNull: false,
comment: "点赞用户id",
// 定义外键(重要)
references: {
model: "user", // 对应表名称(数据表名称)
key: "id", // 对应表的主键
},
onUpdate: "restrict", // 更新时操作
onDelete: "cascade", // 删除时操作
},
moment_id: {
type: INTEGER(20).UNSIGNED,
allowNull: false,
comment: "朋友圈消息id",
// 定义外键(重要)
references: {
model: "moment", // 对应表名称(数据表名称)
key: "id", // 对应表的主键
},
onUpdate: "restrict", // 更新时操作
onDelete: "cascade", // 删除时操作
},
created_at: DATE,
updated_at: DATE,
});
},
down: async (queryInterface) => {
await queryInterface.dropTable("moment_like");
},
};
js
// moment_comment表
"use strict";
module.exports = {
up: async (queryInterface, Sequelize) => {
const { INTEGER, STRING, DATE, ENUM, TEXT } = Sequelize;
// 创建表
await queryInterface.createTable("moment_comment", {
id: {
type: INTEGER(20).UNSIGNED,
primaryKey: true,
autoIncrement: true,
},
user_id: {
type: INTEGER(20).UNSIGNED,
allowNull: false,
comment: "评论用户id",
// 定义外键(重要)
references: {
model: "user", // 对应表名称(数据表名称)
key: "id", // 对应表的主键
},
onUpdate: "restrict", // 更新时操作
onDelete: "cascade", // 删除时操作
},
moment_id: {
type: INTEGER(20).UNSIGNED,
allowNull: false,
comment: "朋友圈消息id",
// 定义外键(重要)
references: {
model: "moment", // 对应表名称(数据表名称)
key: "id", // 对应表的主键
},
onUpdate: "restrict", // 更新时操作
onDelete: "cascade", // 删除时操作
},
content: {
type: TEXT,
allowNull: false,
defaultValue: "",
comment: "评论内容",
},
reply_id: {
type: INTEGER,
allowNull: false,
defaultValue: 0,
comment: "回复用户id 0顶级评论",
},
created_at: DATE,
updated_at: DATE,
});
},
down: async (queryInterface) => {
await queryInterface.dropTable("moment_comment");
},
};
创建模型
js
// app/model/moment.js
"use strict";
module.exports = (app) => {
const { STRING, INTEGER, DATE, ENUM, TEXT } = app.Sequelize;
// 配置(重要:一定要配置详细,一定要!!!)
const Moment = app.model.define("moment", {
id: {
type: INTEGER(20).UNSIGNED,
primaryKey: true,
autoIncrement: true,
},
content: {
type: TEXT,
allowNull: false,
defaultValue: "",
comment: "朋友圈内容",
},
image: {
type: TEXT,
allowNull: false,
defaultValue: "",
comment: "朋友圈图片",
},
video: {
type: STRING,
allowNull: false,
defaultValue: "",
comment: "朋友圈视频",
},
location: {
type: STRING,
allowNull: false,
defaultValue: "",
comment: "位置",
},
remind: {
type: STRING,
allowNull: false,
defaultValue: "",
comment: "提醒谁看",
},
see: {
type: STRING,
allowNull: false,
defaultValue: "all",
comment: "谁可以看 all公开 none私密",
},
user_id: {
type: INTEGER(20).UNSIGNED,
allowNull: false,
comment: "用户id",
// 定义外键(重要)
references: {
model: "user", // 对应表名称(数据表名称)
key: "id", // 对应表的主键
},
onUpdate: "restrict", // 更新时操作
onDelete: "cascade", // 删除时操作
},
created_at: {
type: DATE,
get() {
return (new Date(this.getDataValue("created_at"))).getTime();
},
},
updated_at: DATE,
});
Moment.associate = function (model) {
Moment.hasMany(app.model.MomentComment, {
foreignKey: "moment_id",
});
Moment.hasMany(app.model.MomentLike, {
foreignKey: "moment_id",
});
Moment.belongsTo(app.model.User, {
foreignKey: "user_id",
});
};
return Moment;
};
js
// app/model/moment_timeline.js
"use strict";
module.exports = (app) => {
const { STRING, INTEGER, DATE, ENUM, TEXT } = app.Sequelize;
// 配置(重要:一定要配置详细,一定要!!!)
const MomentTimeline = app.model.define("moment_timeline", {
id: {
type: INTEGER(20).UNSIGNED,
primaryKey: true,
autoIncrement: true,
},
user_id: {
type: INTEGER(20).UNSIGNED,
allowNull: false,
comment: "用户id",
// 定义外键(重要)
references: {
model: "user", // 对应表名称(数据表名称)
key: "id", // 对应表的主键
},
onUpdate: "restrict", // 更新时操作
onDelete: "cascade", // 删除时操作
},
moment_id: {
type: INTEGER(20).UNSIGNED,
allowNull: false,
comment: "朋友圈消息id",
// 定义外键(重要)
references: {
model: "moment", // 对应表名称(数据表名称)
key: "id", // 对应表的主键
},
onUpdate: "restrict", // 更新时操作
onDelete: "cascade", // 删除时操作
},
own: {
type: INTEGER(1),
allowNull: false,
defaultValue: 0,
comment: "是否是自己的 0否1是",
},
created_at: {
type: DATE,
get(val) {
return (new Date(this.getDataValue("created_at"))).getTime();
},
},
updated_at: DATE,
});
MomentTimeline.associate = function (model) {
MomentTimeline.belongsTo(app.model.Moment, {
foreignKey: "moment_id",
});
MomentTimeline.belongsTo(app.model.User, {
foreignKey: "user_id",
});
};
return MomentTimeline;
};
js
// app/model/moment_comment.js
"use strict";
module.exports = (app) => {
const { STRING, INTEGER, DATE, ENUM, TEXT } = app.Sequelize;
// 配置(重要:一定要配置详细,一定要!!!)
const MomentComment = app.model.define("moment_comment", {
id: {
type: INTEGER(20).UNSIGNED,
primaryKey: true,
autoIncrement: true,
},
user_id: {
type: INTEGER(20).UNSIGNED,
allowNull: false,
comment: "评论用户id",
// 定义外键(重要)
references: {
model: "user", // 对应表名称(数据表名称)
key: "id", // 对应表的主键
},
onUpdate: "restrict", // 更新时操作
onDelete: "cascade", // 删除时操作
},
moment_id: {
type: INTEGER(20).UNSIGNED,
allowNull: false,
comment: "朋友圈消息id",
// 定义外键(重要)
references: {
model: "moment", // 对应表名称(数据表名称)
key: "id", // 对应表的主键
},
onUpdate: "restrict", // 更新时操作
onDelete: "cascade", // 删除时操作
},
content: {
type: TEXT,
allowNull: false,
defaultValue: "",
comment: "评论内容",
},
reply_id: {
type: INTEGER,
allowNull: false,
defaultValue: 0,
comment: "回复用户id 0顶级评论",
},
created_at: DATE,
updated_at: DATE,
});
MomentComment.associate = function (model) {
MomentComment.belongsTo(app.model.User, {
foreignKey: "user_id",
as: "momentCommentUser",
});
MomentComment.belongsTo(app.model.User, {
foreignKey: "reply_id",
as: "momentCommentReply",
});
};
return MomentComment;
};
js
// app/model/moment_like.js
"use strict";
module.exports = (app) => {
const { STRING, INTEGER, DATE, ENUM, TEXT } = app.Sequelize;
// 配置(重要:一定要配置详细,一定要!!!)
const MomentLike = app.model.define("moment_like", {
id: {
type: INTEGER(20).UNSIGNED,
primaryKey: true,
autoIncrement: true,
},
user_id: {
type: INTEGER(20).UNSIGNED,
allowNull: false,
comment: "点赞用户id",
// 定义外键(重要)
references: {
model: "user", // 对应表名称(数据表名称)
key: "id", // 对应表的主键
},
onUpdate: "restrict", // 更新时操作
onDelete: "cascade", // 删除时操作
},
moment_id: {
type: INTEGER(20).UNSIGNED,
allowNull: false,
comment: "朋友圈消息id",
// 定义外键(重要)
references: {
model: "moment", // 对应表名称(数据表名称)
key: "id", // 对应表的主键
},
onUpdate: "restrict", // 更新时操作
onDelete: "cascade", // 删除时操作
},
created_at: DATE,
updated_at: DATE,
});
MomentLike.associate = function (model) {
MomentLike.belongsTo(app.model.User, {
foreignKey: "user_id",
});
};
return MomentLike;
};