sequelize Unknown column '' in 'on clause'
Asked Answered
M

1

6

I have the below code and I am having a problem. I'm trying to include the images table...

There is a relation between the order and images already, but I don't know why it's showing the error below:

Unhandled rejection SequelizeDatabaseError: ER_BAD_FIELD_ERROR: Unknown column 'images.imageable' in 'on clause'

below the code :

models.Order.findOne({
    where: {
        id: req.params.orderId
    },
    attributes: ['id', 'comment', 'rating', 'orderDescription', 'userLat', 'userLng', 
                 'orderStatus', 'providerId', 'isUsingBalance', 'userId', 'serviceId', 
                 'createdAt', 'updatedAt', 'ticketNumber', 'orderScheduledDate', 'promoId', 'paymentMethodId',
        [sequelize.Sequelize.fn('date_format', sequelize.Sequelize.col('orderScheduledDate'), '%Y-%m-%D %h:%i'), 'orderScheduledDateToEdit']

    ],
    include: [{
            model: models.Comment,

            include: [{
                model: models.User,
                attributes: ['firstName', 'lastName', 'id']
            }]
        },
        {
            model: models.Provider,
            attributes: ['id', 'userId'],
            include: [{
                    model: models.User,
                    attributes: ['id', 'firstName', 'lastName', 'phoneNumber']
                },
                {
                    model: models.ProviderLocation,
                    attributes: ['lat', 'lng', 'providerId']
                },
                {
                    model: models.PaymentMethod,
                    through: {
                        where: {
                            status: "ACTIVE"
                        }
                    }
                }
            ]
        },
        {
            model: models.User,
            attributes: ['id', 'firstName', 'lastName', 'phoneNumber', 'status'],
            include: [{
                model: models.Balance,
                attributes: ['balance']

            }]
        },
        {
            model: models.notified_providers,
            attributes: ['orderId', 'providerBroadcastedId', 'distance', 'duration', 'status', 'rank_score', 'rate_score', 'distance_score', 'nationality_score', 'total_score'],
            include: [{
                model: models.Provider,
                attributes: ['id'],
                include: [{
                        model: models.User,
                        attributes: ['id', 'firstName', 'lastName', 'phoneNumber', 'status', 'rate', 'rankId']
                    },
                    {
                        model: models.PaymentMethod,
                        through: {
                            where: {
                                status: "ACTIVE"
                            }
                        }
                    }

                ]
            }]
        },
        {
            model: models.rate,
            attributes: ['rate']
        },
        {
            model: models.Service,
            attributes: ['serviceType']
        },
        {
            model: models.Promotion,
            attributes: ['code', 'conditions']
        },
        {
            model: models.PaymentMethod,
            attributes: ['name', 'image']
        },
        {
            model: models.images,
            attributes: ['imageble', 'image', 'imagebleId']
        }
    ]
});

I checked the relation inside each model and it's fine, there's no issue.

This is the Order model:

Order.hasMany(models.images, {
    foreignKey: 'imagebleId',
    scope: {
        imageable: 'Order'
    }
});

And here's the Images model:

images.belongsTo(models.Order, { foreignKey: "imagebleId" });
Musetta answered 9/2, 2019 at 17:43 Comment(6)
are you sure the column name is imageble for image table ??Raspy
Have you a) looked at the table you are accessing b) looked at the raw SQL being sent to DB?Holt
yes i checked the table and the names are correctMusetta
Try adding as attribute on images model association definition and on the include as well.Forficate
I found my answers in you question. Thank you.Population
Adding subquery:false worked for me as mentioned hereSporocyte
M
1

I tried @Ellebkey solution (posted as a comment above)

Try adding as attribute on images model association definition and on the include as well.

using (AS) worked like a charm.

Musetta answered 12/2, 2019 at 13:57 Comment(2)
The reason why as worked is because without it, Sequelize is going to "guess" the name of the column based on default mechanisms. For example, if your models have auto generated ids, it'll try something like OrderId and ImagesId, i.e., based on the names of the models.Pigmentation
adding as didn't work for meCutlass

© 2022 - 2024 — McMap. All rights reserved.