【问题标题】:Sequelize eager loading association with through tableSequelize 急切加载关联与直通表
【发布时间】:2015-02-24 04:27:22
【问题描述】:

我已经在网站上遇到了几个问题,但我仍然看不出我在这里做错了什么,所以任何帮助都将不胜感激。

我收到了错误:

Organization (organizations) is not associated to User!

组织模型:

module.exports = function (sequelize, DataTypes) {
    return sequelize.define('Organization', {

        organizationID: {
            primaryKey: true,
            type: DataTypes.INTEGER(11),
            allowNull: false,
        },
        name: {
            type: DataTypes.STRING,
            allowNull: false,
        },
        description: {
            type: DataTypes.STRING,
            allowNull: true,
        }
    },
    {
        tableName: "spa_vOrganization",
        freezeTableName: true,
        classMethods: {
            associate: function (models) {
                Organization.hasMany(models.User, {
                    as: 'users',
                    through: models.User_Tenant_Organization,
                    foreignKey: 'organizationID'
                });
            }
        },
    });
};

用户模型:

module.exports = function (sequelize, DataTypes) {
    return sequelize.define('User', {
            userID: {
                primaryKey: true,
                type: DataTypes.INTEGER(11),
                allowNull: false,
            },
            password: {
                type: DataTypes.STRING,
                allowNull: false,
            },
            email: {
                type: DataTypes.STRING,
                allowNull: false,
            }
        },
        {
            tableName: "spa_User",
            freezeTableName: true,
            classMethods: {
                associate: function(models) {
                    User.hasMany(models.Organization, { as: "organizations", through: models.User_Tenant_Organization, foreignKey: 'userID'});
                }
            }
        }
    );
};

矩阵表模型:

module.exports = function (sequelize, DataTypes) {
    return sequelize.define('User_Tenant_Organization', {

        userTenantOrganizationID: {
            primaryKey: true,
            type: DataTypes.INTEGER(11),
            allowNull: false,
        },
        userID: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
        },
        organizationID: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
        },
        tenantID: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
        },
    },
    {
        tableName: "spa_User_Tenant_Organization",
        freezeTableName: true,
    });
};

我要做的只是拉回一个用户,他们的组织急切地加载。这是我正在使用的:

models.User.findOne({where: {
        email: body.email
    }, include: [ {model:models.Organization, as: 'organizations'}]}).complete( function (err, user)     {
// do something with the user
}

我已经尝试过在UserOrganization 上使用和不使用foreignKey 定义,没有任何区别。我显然误解了一些关于协会的事情。谁能告诉我哪里出错了?

【问题讨论】:

    标签: node.js associations sequelize.js


    【解决方案1】:

    我发现了问题。上面代码中的关联实际上是正确的——失败的是我的模型/index.js,它是由 yeoman generator-angular-express-sequelize 自动生成的

    index.js 循环遍历模型文件,将它们导入到 sequelize 对象中并将副本存储在数组 db[] 中,然后尝试运行 classMethod associate(),但它正在调用 models.options.associate( ) 而不是 models.associate():

    Object.keys(db).forEach(function (modelName) {
        if (db[modelName].options.hasOwnProperty('associate')) {
            db[modelName].options.associate(db);
        }
    });
    

    我已经通过删除“.options”解决了这个问题,一切正常。

    解决问题的拉取请求在这里供参考:https://github.com/rayokota/generator-angular-express-sequelize/pull/7

    【讨论】:

      猜你喜欢
      • 2018-07-28
      • 1970-01-01
      • 2021-02-07
      • 1970-01-01
      • 1970-01-01
      • 2018-05-29
      • 2019-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多