uni-app 138朋友圈相关数据表迁移

简介: uni-app 138朋友圈相关数据表迁移


创建数据库迁移文件

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;
};

感谢大家观看,我们下次见

目录
相关文章
|
10月前
uni-app 159发布朋友圈-实时通知
uni-app 159发布朋友圈-实时通知
79 0
|
10月前
uni-app 155朋友圈评论功能(二)
uni-app 155朋友圈评论功能(二)
156 0
|
10月前
uni-app 152朋友圈动态实时通知功能
uni-app 152朋友圈动态实时通知功能
69 0
|
5月前
|
JavaScript 小程序 开发者
uni-app开发实战:利用Vue混入(mixin)实现微信小程序全局分享功能,一键发送给朋友、分享到朋友圈、复制链接
uni-app开发实战:利用Vue混入(mixin)实现微信小程序全局分享功能,一键发送给朋友、分享到朋友圈、复制链接
808 0
|
10月前
|
移动开发 前端开发
uni-app 180查看好友朋友圈完善(一)
uni-app 180查看好友朋友圈完善(一)
60 3
|
10月前
uni-app 182查看好友朋友圈完善(三)
uni-app 182查看好友朋友圈完善(三)
42 3
|
10月前
|
移动开发 前端开发
uni-app 184查看好友朋友圈完善(五)
uni-app 184查看好友朋友圈完善(五)
39 2
|
10月前
uni-app 183查看好友朋友圈完善(四)
uni-app 183查看好友朋友圈完善(四)
63 1
|
10月前
uni-app 181查看好友朋友圈完善(二)
uni-app 181查看好友朋友圈完善(二)
37 1
|
10月前
uni-app 151修复删除好友朋友圈记录问题
uni-app 151修复删除好友朋友圈记录问题
51 1

热门文章

最新文章

  • 1
    MNN-LLM App:在手机上离线运行大模型,阿里巴巴开源基于 MNN-LLM 框架开发的手机 AI 助手应用
  • 2
    原生鸿蒙版小艺APP接入DeepSeek-R1,为HarmonyOS应用开发注入新活力
  • 3
    【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
  • 4
    【11】flutter进行了聊天页面的开发-增加了即时通讯聊天的整体页面和组件-切换-朋友-陌生人-vip开通详细页面-即时通讯sdk准备-直播sdk准备-即时通讯有无UI集成的区别介绍-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
  • 5
    【Azure App Service】基于Linux创建的App Service是否可以主动升级内置的Nginx版本呢?
  • 6
    1688APP 原数据 API 接口的开发、应用与收益
  • 7
    PiliPala:开源项目真香,B站用户狂喜!这个开源APP竟能自定义主题+去广告?PiliPala隐藏功能大揭秘
  • 8
    APP-国内主流安卓商店-应用市场-鸿蒙商店上架之必备前提·全国公安安全信息评估报告如何申请-需要安全评估报告的资料是哪些-优雅草卓伊凡全程操作
  • 9
    语音app系统软件源码开发搭建新手启蒙篇
  • 10
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 1
    【Azure Storage Account】利用App Service作为反向代理, 并使用.NET Storage Account SDK实现上传/下载操作
    12
  • 2
    【02】微信支付商户申请下户到配置完整流程-微信开放平台申请APP应用-微信商户支付绑定appid-公众号和小程序分别申请appid-申请+配置完整流程-优雅草卓伊凡
    18
  • 3
    【Azure App Service】App Service 是否支持HostName SNI 证书?
    18
  • 4
    iOS|记一名 iOS 开发新手的前两次 App 审核经历
    14
  • 5
    2025同城线下陪玩APP开发/电竞游戏平台搭建游戏陪玩APP源码/语音APP开发
    24
  • 6
    flutter3-wetrip跨平台自研仿携程app预约酒店系统模板
    28
  • 7
    通过外部链接启动 Flutter App(详细介绍及示例)
    24
  • 8
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    55
  • 9
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    46
  • 10
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
    40