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" });
diogo
  • 3,769
  • 1
  • 24
  • 30
Faisal
  • 441
  • 1
  • 7
  • 18

1 Answers1

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.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Faisal
  • 441
  • 1
  • 7
  • 18
  • 1
    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. – diogo Jul 21 '20 at 21:01
  • adding `as` didn't work for me – Muhammad Uzair Mar 10 '22 at 18:51