创建数据库迁移文件
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
/database/migrations/xxxx-moment.js
'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'); } };
/database/migrations/xxxx-moment_timeline.js
'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'); } };
/database/migrations/xxxx-moment_like.js
'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'); } };
/database/migrations/xxxx-moment_time.js
'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'); } };
执行创建命令
npx sequelize db:migrate
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; };
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; };
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; };
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; };
感谢大家观看,我们下次见