Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include with depth 2 and main where to include table #11889

Open
2 of 7 tasks
sneznaovca opened this issue Jan 31, 2020 · 4 comments
Open
2 of 7 tasks

include with depth 2 and main where to include table #11889

sneznaovca opened this issue Jan 31, 2020 · 4 comments
Labels
type: docs For issues and PRs. Things related to documentation, such as changes in the manuals / API reference.

Comments

@sneznaovca
Copy link

sneznaovca commented Jan 31, 2020

I have table articles with columns id,id_author, id_editor. Second table is users with id and code. Models have relations on both articles columns (hasMany,belongsTo).

I need select all articles if users.code match constant or if both are null. I can't use where in include (or I don't know how for this conditions with OR).

I have something like..and it works correct

let options = {
            include: [
                {
                    model: Author,
                    as: 'Create_ref',
                    attributes: ['code'],
                    required: false,
                },
                {
                    model: Author,
                    as: 'Editor_ref',
                    attributes: ['code'],
                    required: false,
                }
            ],
            where: {
                [Op.and]: [
                    {
                        [Op.or]: [
                            Articles.sequelize.where(Articles.sequelize.col('Create_ref.code'), '=', constant),
                            Articles.sequelize.where(Articles.sequelize.col('Editor_ref.code'), '=', constant),
                             {
                                [Op.and]: [
                                    Articles.sequelize.where(Articles.sequelize.col('Create_ref.code'), '=', null),
                                    Articles.sequelize.where(Articles.sequelize.col('Editor_ref.code'), '=', null),
                                ]
                            }
                        ],
                    }
                    , // more conditions
                ]
            },
        };

but when I add new include with include sequelize generate SQL with subquery and where is inside subquery but joins are outside. Query fail on Unknown column 'Create_ref.code' in 'where clause'

Is it bug or I need different approach?

        let opt = {
            include: [
                {
                    model: Author,
                    as: 'Create_ref',
                    attributes: ['code'],
                    required: false,
                },
                {
                    model: Author,
                    as: 'Editor_ref',
                    attributes: ['code'],
                    required: false,
                },{
                    model: Services,
                    as: 'article_services_hasMany',
                    include: [
                        'service_ref'
                    ]
                },
            ],
            where: {
                [Op.and]: [
                    {
                        [Op.or]: [
                            Articles.sequelize.where(Articles.sequelize.col('Create_ref.code'), '=', constant),
                            Articles.sequelize.where(Articles.sequelize.col('Editor_ref.code'), '=', constant),
                            {
                                [Op.and]: [
                                    Articles.sequelize.where(Articles.sequelize.col('Create_ref.code'), '=', null),
                                    Articles.sequelize.where(Articles.sequelize.col('Editor_ref.code'), '=', null),
                                ]
                            }
                        ],
                    }
                    , // more conditions
                ]
            },
        };

Issue Template Checklist

Is this issue dialect-specific?

  • No. This issue is relevant to Sequelize as a whole.
  • Yes. This issue only applies to the following dialect(s): XXX, YYY, ZZZ
  • I don't know.

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I don't know how to start, I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.

I tried it also on 5.21.3 and 6.0.0-beta 4, same error. I used mysql2 engine.

I found a quick fix. Add separate: true, to last include and split query but it is not a proper solution.

@sneznaovca sneznaovca added the type: docs For issues and PRs. Things related to documentation, such as changes in the manuals / API reference. label Jan 31, 2020
@ralexandr
Copy link

Met same problem

@talal7860
Copy link

Looking for a solution as well

@sneznaovca
Copy link
Author

I probably found a solution, add to include object subQuery: false,

@github-actions
Copy link
Contributor

github-actions bot commented Nov 3, 2021

This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

@github-actions github-actions bot added the stale label Nov 3, 2021
@WikiRik WikiRik removed the stale label Nov 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: docs For issues and PRs. Things related to documentation, such as changes in the manuals / API reference.
Projects
Development

No branches or pull requests

4 participants