Skip to content

Quotes in SQL for included model JSON #7610

Open

Description

@elmeister

Please check the following code, which should return two IDs from PG JSONB fields - for Pipeline and Job.

Pipeline.findAll({
  where: { user_id: id },
  attributes: [[sequelize.json('pipeline_json.id'), 'pipeline_id']],
  raw: true,
  include: [{ model: Job, attributes: [[sequelize.json('job_json.id'), 'job_id']] }],
})

Something like the following SQL code is expected, with "job_json" #>> '{id}'

SELECT
  pipeline_json #>> '{id}'  AS "pipeline_id",
  "job"."job_json" #>> '{id}' AS "job_id"
FROM "pipeline" AS "pipeline" LEFT OUTER JOIN "job" AS "job" ON "pipeline"."job_id" = "job"."id"
WHERE "pipeline"."user_id" = 123;

But actually the following SQL is produced, with "job_json#>>'{id}'"

SELECT
  pipeline_json #>> '{id}'  AS "pipeline_id",
  "job"."job_json#>>'{id}'" AS "job_id"
FROM "pipeline" AS "pipeline" LEFT OUTER JOIN "job" AS "job" ON "pipeline"."job_id" = "job"."id"
WHERE "pipeline"."user_id" = 123;

Which obviously followed by error SequelizeDatabaseError: column job.job_json#>>'{id}' does not exist
Please share if there is any workaround for this case.

Activity

  1. added
    type: bug
    on May 14, 2017
  2. mkaufmaner commented on May 18, 2017

    @mkaufmaner
    Contributor

    @elmeister What version of Sequelize are you using?

  3. sushantdhiman commented on May 19, 2017

    @sushantdhiman
    Contributor

    I have verified this bug exists on current master

  4. elmeister commented on May 19, 2017

    @elmeister
    Author
  5. stale commented on Jul 18, 2017

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

  6. elmeister commented on Jul 18, 2017

    @elmeister
    Author

    It is still an issue actually.

  7. self-assigned this
    on Aug 8, 2017
  8. sm2017 commented on Oct 6, 2021

    @sm2017

    @elmeister Do you have a solution?
    @sequelize Please fix it

  9. added theissue type on Mar 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type: bug

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions