首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Sequelize.js -“不关联于”

Sequelize.js -“不关联于”
EN

Stack Overflow用户
提问于 2018-06-13 16:17:01
回答 2查看 25.4K关注 0票数 14

我对从db获得完整的数据有一些问题。这就是我的模特:

用户

代码语言:javascript
复制
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'
    });
};

角色

代码语言:javascript
复制
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',
});};

我想获得一个特定用户的对象,包括所有角色内容。有些人认为

代码语言:javascript
复制
{
  id: 4,
  password: 'xxx',
  email: 'adsads@saas.com',
  role: {
     id: 2,
     name: 'admin'
     description: 'ipsum ssaffa',
     permission: 30
  }
}

所以我用:

代码语言:javascript
复制
User.findOne( { where: { id: req.userId }, include: [ Role ] } ).then( user =>{...});

但是我得到了结果err.message:“角色与用户无关”

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

*为了处理模型,我使用了sequelize

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-13 17:11:50

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

基于您的json,我看到每个用户只有一个角色,所以您可以在角色模型中使用,也可以在用户模型中使用hasOne。

应该是这样的:

User.js

代码语言:javascript
复制
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

代码语言:javascript
复制
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;
};
票数 10
EN

Stack Overflow用户

发布于 2021-08-15 11:45:59

你必须声明你的模型之间的关联。如果使用Sequelize,请确保调用静态方法关联。示例:

/models.index.js

代码语言:javascript
复制
const Category  = require('./Category');
const Product = require('./Product');
const ProductTag = require('./ProductTag');
const Tag = require('./Tag');

Category.associate({Product});
Product.associate({Category,Tag});
Tag.associate({Product});

module.exports={Category,Product,ProductTag,Tag};

然后Category.js中的关联

代码语言:javascript
复制
'use strict';
const {Model,DataTypes} = require('sequelize');
const sequelize = require('../config/connection.js');

    class Category extends Model {
        /**
         * Helper method for defining associations.
         * This method is not a part of Sequelize lifecycle.
         * The `models/index` file will call this method.
         */
        static associate({Product}) {
            // define association here
            console.log('Category associated with: Product');
            this.hasMany(Product, {
                foreignKey: 'category_id',
                onDelete: 'CASCADE'
            });
        }
    }

    Category.init({
        category_id: {type: DataTypes.INTEGER, autoIncrement: true, allowNull: false, primaryKey: true},
        category_name: {type: DataTypes.STRING, allowNull: false}
    }, {
        sequelize,
        timestamps: false,
        freezeTableName: true,
        underscored: true,
        modelName: "Category",
    });

module.exports = Category;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50841912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档