question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Limit causing "missing FROM-clause entry for table"

See original GitHub issue

Hi,

Got an issue with sequelize, tried 3.5 and 3.7.1, using postgres. I added as much detail as I could below, but the short version is that adding limit to a query with an include results in a different query than expected.

I simplified the models for readability, but its just a simple job system. Lets say the job is to run echo 'Hello World' than a Run would be status/result of the execution. You can run a job multiple times resulting in a new Run model. During execution the system may add RunMessages to the Run, which can be the output of the command.

What I’m trying to query is the answer to the following question: For a given job what are the last 10 runs and the number of messages for each run

The result should be something like

[
 {"id":"...","status": "done","jobid":"...", ... , "msg_count":3},
 {"id":"...","status": "done","jobid":"...", ... , "msg_count":9},
 {"id":"...","status": "done","jobid":"...", ... , "msg_count":0},
 ...
]

Working query without limit

Query options:

{
  "where" : {
    "jobid":req.params.jobid
  },
  "attributes":
    Object.keys(this.db.model("Run").attributes).concat([
      [sequelize.fn('COUNT',sequelize.col('messages.id')),"msg_count"]
    ]),
  "include":[
      {
        "model": this.db.model("RunMessage"),
        "as" : "messages",
        "attributes":[]
      }
  ],
  "group":['"Run.id"'],
  "order":[["createdAt","DESC"]],
  "logging": console.log,
}

Query

SELECT 
  "Run"."id", 
  "Run"."status", 
  "Run"."jobid",
  "Run"."createdAt", 
  "Run"."updatedAt", 
  COUNT("messages"."id") AS "msg_count"
FROM "Runs" AS "Run" 
LEFT OUTER JOIN "RunMessages" AS "messages" ON "Run"."id" = "messages"."runid" 
WHERE "Run"."jobid" = '607169ba-e6ec-4efa-8aca-b70ffc1d2ffb' 
GROUP BY "Run"."id" 
ORDER BY "Run"."createdAt" DESC;

Adding Limit

{
  "where" : {
    "jobid":req.params.jobid
  },
  "attributes":
    Object.keys(this.db.model("Run").attributes).concat([
      [sequelize.fn('COUNT',sequelize.col('messages.id')),"msg_count"]
    ]),
  "include":[
      {
        "model": this.db.model("RunMessage"),
        "as" : "messages",
        "attributes":[]
      }
  ],
  "group":['"Run.id"'],
  "order":[["createdAt","DESC"]],
  "logging": console.log,
  "limit": 10
}

Query

SELECT 
  "Run".* 
FROM (
  SELECT 
    "Run"."id", 
    "Run"."status", 
    "Run"."jobid", 
    "Run"."createdAt", 
    "Run"."updatedAt", 
    COUNT("messages"."id") AS "msg_count" 
  FROM "Runs" AS "Run" 
  WHERE "Run"."jobid" = '607169ba-e6ec-4efa-8aca-b70ffc1d2ffb' 
  GROUP BY "Run"."id"
  ORDER BY "Run"."createdAt" DESC
  LIMIT 10
) AS "Run" 
LEFT OUTER JOIN "RunMessages" AS "messages" ON "Run"."id" = "messages"."runid"
ORDER BY "Run"."createdAt" DESC;

Error

SequelizeDatabaseError: missing FROM-clause entry for table "messages"
job_1 |     at Query.formatError (/api-jobs/node_modules/sequelize/lib/dialects/postgres/query.js:433:14)
job_1 |     at null.<anonymous> (/api-jobs/node_modules/sequelize/lib/dialects/postgres/query.js:108:19)
job_1 |     at emit (events.js:107:17)
job_1 |     at Query.handleError (/regressinator-api-jobs/node_modules/pg/lib/query.js:99:8)
job_1 |     at null.<anonymous> (/regressinator-api-jobs/node_modules/pg/lib/client.js:166:26)
job_1 |     at emit (events.js:107:17)
job_1 |     at Socket.<anonymous> (/regressinator-api-jobs/node_modules/pg/lib/connection.js:109:12)
job_1 |     at Socket.emit (events.js:107:17)
job_1 |     at readableAddChunk (_stream_readable.js:163:16)
job_1 |     at Socket.Readable.push (_stream_readable.js:126:10)
job_1 |     at TCP.onread (net.js:538:20)

Expected Query

SELECT 
  "Run"."id", 
  "Run"."status", 
  "Run"."jobid",
  "Run"."createdAt", 
  "Run"."updatedAt", 
  COUNT("messages"."id") AS "msg_count"
FROM "Runs" AS "Run" 
LEFT OUTER JOIN "RunMessages" AS "messages" ON "Run"."id" = "messages"."runid" 
WHERE "Run"."jobid" = '607169ba-e6ec-4efa-8aca-b70ffc1d2ffb' 
GROUP BY "Run"."id" 
ORDER BY "Run"."createdAt" DESC
LIMIT 10;

Models

Run

db.define(
    // Model name
    "Run",
    // Properties
    /** @lends Run.prototype */
    {
      /**
      * Identifier of the Job/Run
      * @readonly
      * @type {UUID}
      */
      id : {
        type: DataTypes.UUID,
        //Use V4 UUIDs
        defaultValue: DataTypes.UUIDV4,
        allowNull: false,
        primaryKey: true
      },
      /**
      * Actual status of the job
      * @type {"created"|"running"|"done"|"failed"}
      */
      status : {
        type: DataTypes.ENUM("created","running","done","failed"),
        defaultValue: "created",
        allowNull: false
      },
      /**
      * Job that we should run
      * @type {UUID}
      */
      jobid : {
        type: DataTypes.UUID,
        allowNull:false
      },
    }
  );

RunMessage

db.define(
    // Model name
    "RunMessage",
    // Properties
    /** @lends RunMessage.prototype */
    {
      /**
      * Identifier of the Job/RunMessage
      * @readonly
      * @type {UUID}
      */
      id : {
        type: DataTypes.UUID,
        //Use V4 UUIDs
        defaultValue: DataTypes.UUIDV4,
        allowNull: false,
        primaryKey: true
      },
      /**
      * Run associated with this RunMessage
      * @type {UUID}
      */
      runid : {
        type: DataTypes.UUID,
        allowNull:false
      },
      /**
      * A textual description of the notification
      *
      * @type {Text}
      */
      message : {
        type: DataTypes.TEXT,
        allowNull:false
      },
    },
  );

Association

db.model("Run").hasMany(
  db.model("RunMessage"),
  {
    "as" : "messages",
    "foreignKey" : "runid",
    "onDelete" : "cascade",
    "hooks" : true
  }
);

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:13
  • Comments:19 (6 by maintainers)

github_iconTop GitHub Comments

91reactions
mickhansencommented, Sep 7, 2015

You can try using include.duplicating = false in that case.

17reactions
PorterKcommented, Sep 24, 2018

duplicating: false fixed my query but I’m still left very confused. What does this option do? I can’t seem to find it in the docs anywhere… hmm

Read more comments on GitHub >

github_iconTop Results From Across the Web

limit option produces 'missing FROM-clause entry for table' error
Issue Description findOne produces 'missing FROM-clause entry for table' error on PostgreSQL. What are you doing? User.js module.exports ...
Read more >
missing FROM-clause entry for table - Stack Overflow
The error is related to '$forsale.id$' and $fortrade.is$. – user938363 · 1. Try to indicate subQuery: false in the main options of the...
Read more >
Fix “ERROR: missing FROM-clause entry for table” in ...
Another way to fix it is to use an alias for the column: (SELECT TeacherName t FROM Teachers) UNION (SELECT StudentName FROM Students)...
Read more >
Knex error: missing FROM-clause entry for table-postgresql
Coding example for the question Knex error: missing FROM-clause entry for table-postgresql.
Read more >
Bug #8817: PGError: ERROR: missing FROM-clause entry for ...
After migrating from Foreman 1.6.3 to 1.7.1, users that are not an administrator receive the above error line upon login (full error is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found