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.

Postgresql update + joins, from clause missing

See original GitHub issue

Hi,

I hope to rewrite a raw SQL UPDATE/JOINS request to Knex style, but I have an issue with knex : The knex raw request (works well) :

knex.raw("UPDATE barcodes_products B SET bar_code = $6, modify_date = now() " +
                "FROM catalogs_stores S " +
                "  INNER JOIN catalogs C on (S.catalog_id = C.catalog_id AND S.company_id = C.company_id AND C.status = 1) " +
                    "  INNER JOIN catalogsversion R on (R.catalog_id = C.catalog_id AND R.status = 1) " +
                    "  INNER JOIN catalogsversion_products V on (R.catalogversion_id = V.catalogversion_id AND C.catalog_id = R.catalog_id AND V.status = 1) " +
                    "  INNER JOIN products P on (V.product_id = P.product_id AND V.company_id = P.company_id AND P.status = 1) " +
                    "WHERE S.store_id_ref = $1 " +
                    "  AND S.status = 1 " +
                    "  AND C.catalog_id = $2 " +
                    "  AND V.catalogversion_id = $3 " +
                    "  AND R.valid_from < now() " +
                    "  AND (R.valid_until IS NULL OR R.valid_until > now()) " +
                    "  AND P.product_id = $4 " +
                    "  AND B.bar_code = $5 " +
                    "  AND P.purchase_ok = true " +
                    "  AND P.product_id = B.product_id " +
                    "  AND P.company_id = B.company_id " +
                    "  AND B.status = 1",
                    [store, catalog, version, product, barcode_current, barcode_new]
                    )
                    .exec(cb);

The Knex rewrited “style” request :

knex('barcodes_products AS B')
             .from('catalogs_stores AS S')
            .innerJoin('catalogs AS C', function () {
                this.on('S.catalog_id', 'C.catalog_id');
                this.andOn('S.company_id', 'C.company_id');
                this.andOn('C.status', 1);
            })
            .innerJoin('catalogsversion AS R', function () {
                this.on('R.catalog_id', 'C.catalog_id');
                this.andOn('R.status', 1);
            })
            .innerJoin('catalogsversion_products AS V', function () {
                this.on('R.catalogversion_id', 'V.catalogversion_id');
                this.andOn('R.catalog_id', 'V.catalog_id');
                this.andOn('V.status', 1);
            })
            .innerJoin('products AS P', function () {
                this.on('P.product_id', 'V.product_id');
                this.andOn('P.company_id', 'V.company_id');
                this.andOn('P.status', 1);
            })
            .where({
                'S.store_id_ref': store,
                'C.catalog_id': catalog,
                'V.catalogversion_id': version,
                'P.product_id': product,
                'B.barcode': barcode_current
            })
            .andWhere('P.purchase_ok', true)
            .andWhere('B.status', 1)
            .andWhere('P.product_id', 'B.product_id')
            .andWhere('P.company_id', 'B.company_id')
            .andWhere('R.valid_from', '<', 'now()')
            .andWhere(function () {
                this.whereNull('R.valid_until');
                this.orWhere('R.valid_until', '>', 'now()');
            }).update({
                'bar_code': barcode_new,
                'modify_date': 'now()'
            })
            .exec(cb);
        }

Knex generated this SQL statement :

update "catalogs_stores" as "S" set "bar_code" = $1, "modify_date" = $2 where "S"."store_id_ref" = $3 and "C"."catalog_id" = $4 and "V"."catalogversion_id" = $5 and "P"."product_id" = $6 and "B"."barcode" = $7 and "P"."purchase_ok" = $8 and "B"."status" = $9 and "P"."product_id" = $10 and "P"."company_id" = $11 and "R"."valid_from" < $12 and ("R"."valid_until" is null or "R"."valid_until" > $13)

Postgresql respond: missing FROM-clause entry for table "C"

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:2
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

7reactions
nilgradisnikcommented, Jun 15, 2016

@bendrucker @tgriesser Any updates on this issue? This seems to be a pretty common use case, resorting to raw queries makes me doubt the use for knex.

1reaction
MarcFRICOUcommented, Aug 24, 2016

Same problem for me : I need to check a value into db before the update. I’ve hoped i won’t be forced to do 2 queries but with this issue, I have no choice 😢:

knex('table')
      .from('table AS oldValues')
      .where('id', '=', 'id')
      .where('table.id', '=', 'oldValues.id')
      .update(['status', 'newStatus'])
      .returning(['table.*', 'oldValues.status AS oldStatus'])
      .toString();

should return

UPDATE "table" SET "status" = 'newStatus' FROM "table" AS oldValues WHERE "table"."id" = 'id' AND "table"."id" = "oldTable"."id" RETURNING "table".*, "oldValues"."status" AS "oldStatus"
Read more comments on GitHub >

github_iconTop Results From Across the Web

Updating records missing from clause - Stack Overflow
With this query, I'm getting an error. ERROR: missing FROM-clause ...
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 >
Re: " Adding missing FROM-clause entry for table .... " problem.
Only if a new syntax is added. > > For UPDATE, we actually do have an extension syntax that allows naming. > secondary...
Read more >
How to handle a missing 'FROM' clause entry for a table an ...
The PostgreSQL UPDATE statement has a non-standard FROM clause. Standard SQL allows the UPDATE…WHERE clause to use set predicates and sub-queries instead.
Read more >
An overview of the SQL Server Update Join - SQLShack
We can use the Update statement with Left Join as well, and it updates the records with NULL values. As highlighted earlier, we...
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