Sequelize Association Error Cannot read property 'getTableName' of undefined

后端 未结 1 1527
轮回少年
轮回少年 2021-01-12 10:05

I am running into an issue where I get an error message, Unhandled rejection TypeError: Cannot read property \'getTableName\' of undefined when I try to associa

1条回答
  •  终归单人心
    2021-01-12 11:03

    You need to rewrite your query to look like:

    models.DiscoverySource.findAll({
        attributes: ['discoverySource'],
        where: { 
            organizationId: req.user.organizationId
        },
        include: [{
            model: models.Organization,
            attributes: ['organizationName', 'admin']
        }]
    })
    

    According to the Sequelize documentation:

    [options.attributes] - A list of the attributes that you want to select, or an object with include and exclude keys.

    [options.include[].attributes] - A list of attributes to select from the child model.

    [options.include[].through.where] - Filter on the join model for belongsToMany relations.

    [options.include[].through.attributes] - A list of attributes to select from the join model for belongsToMany relations.

    So, [options.include[].through] can only be used in case of Belongs-To-Many association rather than Belong-To used by you for DiscoverySource and Organization models.

    0 讨论(0)
提交回复
热议问题