zoukankan      html  css  js  c++  java
  • Eggjs 联表查询

    实现:

    通过 user表 关联外键 查询 role表 及 diary表

    1.数据模型

    app/model/user.js

    /**
     * 用户模型
     */
    module.exports = app => {
      const { STRING, INTEGER } = app.Sequelize;
      const User = app.model.define('user', {
        id: {
          type: INTEGER,
          autoIncrement: true,
          primaryKey: true
        },
        name: {
          type: STRING,
          allowNull: false
        },
        email: {
          type: STRING,
          allowNull: false
        },
        password: {
          type: STRING(32),
          allowNull: false
        },
        avatar: {
          type: STRING,
          allowNull: true
        },
        roleId: {
          type: INTEGER,
          allowNull: false,
          defaultValue: 6
        }
      });
    
      // 表关联的字段
      User.associate = function() {
        // 一对多
        app.model.User.hasMany(app.model.Diary, { foreignKey: 'user_id', targetKey: 'id'});
        /**
         * User.belongsTo(关联的模型, { foreignKey: '使用什么字段关联', targetKey: '与关联的模型那个字段关联', as: '别名' });
        */
        // 一对一
        app.model.User.belongsTo(app.model.Role, { foreignKey: 'roleId', targetKey: 'id', as: 'role'});
      }
    
      return User;
    }

    app/model/role.js

    /**
     * 角色模型
     */
    module.exports = app => {
      const { INTEGER, STRING } = app.Sequelize;
      const Role = app.model.define('role', {
        id: {
          type: INTEGER,
          primaryKey: true,
          autoIncrement: true
        },
        name: STRING(50),
        pid: INTEGER
      }, {
        timestamps: false
      });
     
      return Role;
    }

    app/model/diary.js

    /**
     * 日志模型
     */
    module.exports = app => {
      const { STRING, INTEGER } = app.Sequelize;
      const Diary = app.model.define('diary', {
        id: {
          type: INTEGER,
          autoIncrement: true,
          primaryKey: true
        },
        title: {
          type: STRING,
          allowNull: false
        },
        content: {
          type: STRING,
          allowNull: false
        }
      });
    
      // 表关联的字段
      Diary.associate = function() {
        app.model.Diary.belongsTo(app.model.User, { foreignKey: 'user_id', targetKey: 'id'})
      }
    
      return Diary;
    }
    

    2.关联查询

    app/service/user.js

    // 获取用户列表/详情
    async detail(id) {
      const { app } = this;
      try {
        if(id){
          // return await app.model.User.findByPk(app.toInt(id));
          return await app.model.User.findOne({
            attributes: ['id','name','email','avatar','roleId'],
            where: { id },
            include: [{
              model: app.model.Role,
              as: 'role',//这里的 as需要与之前定义的as名字相同
            }]
          });
        }
        return await app.model.User.findAll({
          attributes: ['id','name','email','avatar','roleId'],
          include: [{
            model: app.model.Role,
            as: 'role',//这里的 as需要与之前定义的as名字相同
          },{
            model: app.model.Diary
          }]
        });
      }catch(err){
        console.log(err);
        return null;
      }
    }
    

    .

  • 相关阅读:
    spring boot2X集成spring cloud config
    Spring boot配置文件application.properties和bootstrap.properties的区别
    (原)linux下caffe模型转tensorflow模型
    (原)torch7中指定可见的GPU
    (原)使用tensorboard显示loss
    (原)tensorflow保存模型及载入保存的模型
    (原)ubuntu挂载及开机自动挂载网络端的文件夹的方法
    (原+转)win7上编译caffe支持python及matlab
    (原)ubuntnu中anaconda的g++提示crtbeginS.o:unrecognized relocation
    (原)PyTorch中使用指定的GPU
  • 原文地址:https://www.cnblogs.com/crazycode2/p/12519557.html
Copyright © 2011-2022 走看看