Faisal
Faisal

Reputation: 441

sequelize Not unique table/alias

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']
                }]
            }
        ]
    })

Upvotes: 0

Views: 3251

Answers (1)

Vivek Doshi
Vivek Doshi

Reputation: 58543

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']
    }]
}]

Upvotes: 2

Related Questions