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" });
as
attribute onimages
model association definition and on the include as well. – Forficatesubquery:false
worked for me as mentioned here – Sporocyte