ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-12-08 09:30:01  阅读:146  来源: 互联网

标签:comment app 数据表 moment 朋友圈 INTEGER model type id


创建数据库迁移文件

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

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

标签:comment,app,数据表,moment,朋友圈,INTEGER,model,type,id
来源: https://blog.csdn.net/ab15176142633/article/details/121487039

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有