sequelize Not unique table/alias

倾然丶 夕夏残阳落幕 提交于 2019-12-24 20:49:16

问题


Dears am getting a problem because included join a table throw another table then i include it immediately

The (User) table should return provider name if i access the (User) table through (Provider)

if included the (User) table imdditly that's mean getting the customer name

but i get the below error

ER_NONUNIQ_TABLE: Not unique table/alias: 'Provider.User'

Code :

models.Order.findOne({

        where: {

            id: req.params.id



        },attributes: ['orderStatus','id','serviceId','providerId','orderDescription',"orderScheduledDate",'userLat','userLng','createdAt'],
        include: [
            {
                model: models.Provider,
                attributes: ['id','userId'],
                include : [{

                    model : models.User,
                    attributes: ['firstName','lastName','phoneNumber']

                },{


                    model : models.User,
                    attributes: ['phoneNumber']
                }]
            }
        ]
    })

回答1:


If you want to include same model twice , you need to assign an alias to the relation-ship/association :

Provider.belongsTo/haveMany/any...(User, {as: 'ProviderUser'}); //<------ HERE
Provider.belongsTo/haveMany/any...(User, {as: 'User'}); //<------ HERE

include: [{
    model: models.Provider,
    attributes: ['id', 'userId'],
    include: [{
        model: models.User,
        as : 'User' //<---------- HERE
        attributes: ['firstName', 'lastName', 'phoneNumber']
    }, {
        model: models.User,
        as : 'ProviderUser' //<---------- HERE
        attributes: ['phoneNumber']
    }]
}]


来源:https://stackoverflow.com/questions/53491063/sequelize-not-unique-table-alias

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!