ORM: Sequelize: I want to change mySQL query to Sequelize.js query.

Asked 1 years ago, Updated 1 years ago, 143 views

A query is a self-joined sentence with one table as follows:

SELECT cat.cat_id, cat.cat_name, sub_cat.cat_id, sub_cat.cat_name
FROM Categories AS cat
LEFT JOIN Categories AS sub_cat
ON sub_cat.parent = cat.cat_id;

Even if you look at the official document, you can name it in the column, but you can't name it on the table. I need help ㅠ<

orm sequelize.js mysql sql database

2022-09-22 13:12

1 Answers

So subcategories find their own categories, right? I found out that Sequelize has include option. I'm not sure if this will be a hint.

// This code has not been tested. It may not work.
models.categories.find({
    include: [
        {
            model: models.categories,
            as: 'sub_cat'
        }
    ],
    where: {
        cat_id: sub_cat.parent
    }
}).then(function(result) { /* something... */ });

If it doesn't work out, I think you can fly Nalquery .


2022-09-22 13:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.