<i id='MfqKU'><tr id='MfqKU'><dt id='MfqKU'><q id='MfqKU'><span id='MfqKU'><b id='MfqKU'><form id='MfqKU'><ins id='MfqKU'></ins><ul id='MfqKU'></ul><sub id='MfqKU'></sub></form><legend id='MfqKU'></legend><bdo id='MfqKU'><pre id='MfqKU'><center id='MfqKU'></center></pre></bdo></b><th id='MfqKU'></th></span></q></dt></tr></i><div id='MfqKU'><tfoot id='MfqKU'></tfoot><dl id='MfqKU'><fieldset id='MfqKU'></fieldset></dl></div>
  • <small id='MfqKU'></small><noframes id='MfqKU'>

      <legend id='MfqKU'><style id='MfqKU'><dir id='MfqKU'><q id='MfqKU'></q></dir></style></legend>
        <bdo id='MfqKU'></bdo><ul id='MfqKU'></ul>
    1. <tfoot id='MfqKU'></tfoot>

        Sequelize.js - “不关联到"

        时间:2023-09-04
          <bdo id='YOIyZ'></bdo><ul id='YOIyZ'></ul>

          <i id='YOIyZ'><tr id='YOIyZ'><dt id='YOIyZ'><q id='YOIyZ'><span id='YOIyZ'><b id='YOIyZ'><form id='YOIyZ'><ins id='YOIyZ'></ins><ul id='YOIyZ'></ul><sub id='YOIyZ'></sub></form><legend id='YOIyZ'></legend><bdo id='YOIyZ'><pre id='YOIyZ'><center id='YOIyZ'></center></pre></bdo></b><th id='YOIyZ'></th></span></q></dt></tr></i><div id='YOIyZ'><tfoot id='YOIyZ'></tfoot><dl id='YOIyZ'><fieldset id='YOIyZ'></fieldset></dl></div>

            <tbody id='YOIyZ'></tbody>
        • <legend id='YOIyZ'><style id='YOIyZ'><dir id='YOIyZ'><q id='YOIyZ'></q></dir></style></legend>

              <small id='YOIyZ'></small><noframes id='YOIyZ'>

              <tfoot id='YOIyZ'></tfoot>

                  本文介绍了Sequelize.js - “不关联到"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在从 db 获取完整数据时遇到了一些问题.那是我的模型:

                  I have some issue with getting full data from db. That are my models:

                  用户

                  module.exports = function(sequelize, DataTypes) {
                      return sequelize.define('user', {
                          id: {
                              type: DataTypes.INTEGER(11),
                              allowNull: false,
                              primaryKey: true,
                              autoIncrement: true,
                              field: 'ID'
                          },
                          password: {
                              type: DataTypes.STRING(255),
                              allowNull: false,
                              field: 'password'
                          },
                          email: {
                              type: DataTypes.STRING(255),
                              allowNull: false,
                              unique: true,
                              field: 'email'
                          },
                          roleId: {
                              type: DataTypes.INTEGER(11),
                              allowNull: false,
                              references: {
                                  model: 'role',
                                  key: 'ID'
                              },
                              field: 'role_id'
                          }
                      }, {
                          timestamps: false,
                          tableName: 'user'
                      });
                  };
                  

                  角色

                  module.exports = function(sequelize, DataTypes) {
                  return sequelize.define('role', {
                      id: {
                          type: DataTypes.INTEGER(11),
                          allowNull: false,
                          primaryKey: true,
                          autoIncrement: true,
                          field: 'ID'
                      },
                      name: {
                          type: DataTypes.STRING(255),
                          allowNull: false,
                          unique: true,
                          field: 'name'
                      },
                      description: {
                          type: DataTypes.STRING(255),
                          allowNull: false,
                          field: 'description'
                      },
                      permission: {
                          type: DataTypes.INTEGER(11),
                          allowNull: false,
                          field: 'permission'
                      }
                  }, {
                      timestamps: false,
                      tableName: 'role',
                  });};
                  

                  我想获取一个特定用户的对象,包括所有角色内容.有点像

                  I want to get object of one specific user including all role content. Somethink like

                  {
                    id: 4,
                    password: 'xxx',
                    email: 'adsads@saas.com',
                    role: {
                       id: 2,
                       name: 'admin'
                       description: 'ipsum ssaffa',
                       permission: 30
                    }
                  }
                  

                  所以我正在使用:

                  User.findOne( { where: { id: req.userId }, include: [ Role ] } ).then( user =>{...});
                  

                  但我得到了结果 err.message: "role is not associated to user"

                  but I get in the result err.message: "role is not associated to user"

                  还有一个简单的问题 - 怎么了?:)

                  And the simple question - what's wrong ? :)

                  *处理我正在使用 sequelize-cli 的模型

                  *to handle models I'm using sequelize-cli

                  推荐答案

                  你得到这个错误是因为你没有在模型之间添加关联

                  You get this error because you didn't add associate between the models

                  根据你的json我看到每个用户只有一个角色,所以你可以在角色模型中使用belongsTo或在用户模型中使用hasOne

                  应该是这样的:

                  User.js

                  module.exports = function(sequelize, DataTypes) {
                  var user =  sequelize.define('user', {
                      id: {
                          type: DataTypes.INTEGER(11),
                          allowNull: false,
                          primaryKey: true,
                          autoIncrement: true,
                          field: 'ID'
                      },
                      password: {
                          type: DataTypes.STRING(255),
                          allowNull: false,
                          field: 'password'
                      },
                      email: {
                          type: DataTypes.STRING(255),
                          allowNull: false,
                          unique: true,
                          field: 'email'
                      },
                      roleId: {
                          type: DataTypes.INTEGER(11),
                          allowNull: false,
                          references: {
                              model: 'role',
                              key: 'ID'
                          },
                          field: 'role_id'
                      }
                  }, {
                      timestamps: false,
                      tableName: 'user'
                  });
                      user.associate = function(models) {
                          user.hasOne(models.role, {foreignKey: 'id',sourceKey: 'roleId'});
                  
                      }
                      return user;
                  };
                  

                  Role.js

                  module.exports = function(sequelize, DataTypes) {
                      var role = sequelize.define('role', {
                      id: {
                          type: DataTypes.INTEGER(11),
                          allowNull: false,
                          primaryKey: true,
                          autoIncrement: true,
                          field: 'ID'
                      },
                      name: {
                          type: DataTypes.STRING(255),
                          allowNull: false,
                          unique: true,
                          field: 'name'
                      },
                      description: {
                          type: DataTypes.STRING(255),
                          allowNull: false,
                          field: 'description'
                      },
                      permission: {
                          type: DataTypes.INTEGER(11),
                          allowNull: false,
                          field: 'permission'
                      }
                      }, {
                          timestamps: false,
                          tableName: 'role',
                      });
                      role.associate = function(models) {
                          user.belongsTo(models.role, {foreignKey: 'id'});
                  
                      }
                      return role;
                  };
                  

                  这篇关于Sequelize.js - “不关联到"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在where子句中将强制转换列序列化为整数 下一篇:错误 [ERR_UNSUPPORTED_DIR_IMPORT]:尝试在本地启动 Nodejs 应用程序时导入目录

                  相关文章

                  <tfoot id='Ul7Hu'></tfoot>

                    <bdo id='Ul7Hu'></bdo><ul id='Ul7Hu'></ul>

                  <i id='Ul7Hu'><tr id='Ul7Hu'><dt id='Ul7Hu'><q id='Ul7Hu'><span id='Ul7Hu'><b id='Ul7Hu'><form id='Ul7Hu'><ins id='Ul7Hu'></ins><ul id='Ul7Hu'></ul><sub id='Ul7Hu'></sub></form><legend id='Ul7Hu'></legend><bdo id='Ul7Hu'><pre id='Ul7Hu'><center id='Ul7Hu'></center></pre></bdo></b><th id='Ul7Hu'></th></span></q></dt></tr></i><div id='Ul7Hu'><tfoot id='Ul7Hu'></tfoot><dl id='Ul7Hu'><fieldset id='Ul7Hu'></fieldset></dl></div>

                      <legend id='Ul7Hu'><style id='Ul7Hu'><dir id='Ul7Hu'><q id='Ul7Hu'></q></dir></style></legend>

                      <small id='Ul7Hu'></small><noframes id='Ul7Hu'>