通过表续集渴望加载关联

时间:2014-12-27 04:07:20

标签: node.js associations sequelize.js

我在网站上经历过几个问题,但我仍然无法看到我在这里做错了什么,所以任何帮助都会非常感激。

我收到错误:

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
}

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

1 个答案:

答案 0 :(得分:1)

我发现了问题。上面代码中的关联实际上是正确的 - 失败的是我的models / index.js,它是由yeoman生成器自动生成的 - 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