3

So, I have the following code that updates a language string table, based on an id, and a language code. If I perform a SELECT using the same from expression, it selects just one row. If I do update, it updates all rows. Where am I going wrong with this?

UPDATE shopmaster.catalog_lang SET shortname='TEST' 
FROM shopmaster.catalog_lang cl LEFT JOIN shopmaster.lang l ON cl.langid=l.langid 
WHERE cl.catalogid=7 AND l.code='fr';

Here's the definition of the two tables:

CREATE TABLE IF NOT EXISTS shopmaster.lang(
    langid SERIAL,
    name TEXT,
    code TEXT,
    active BOOLEAN,
    donotdelete BOOLEAN,
    PRIMARY KEY (langid)


CREATE TABLE IF NOT EXISTS shopmaster.catalog_lang(
    catalogid INT references shopmaster.catalog(catalogid),
    langid INT references shopmaster.lang(langid),
    title TEXT,
    shortname TEXT,
    dirname TEXT,
    PRIMARY KEY (catalogid, langid)
);
2
  • 2
    Quote from the manual "Note that the target table must not appear in the from_list, unless you intend a self-join". The SELECT equivalent of what you are doing is select * from catalog_lang cross join catalog_lang left join lang on ... Commented Aug 24, 2018 at 10:49
  • BTW: why the LEFT join? (combined with WHERE ... AND l.code='fr'; Commented Aug 24, 2018 at 10:54

1 Answer 1

3

Don't repeat the table being updated in the FROM. So:

UPDATE shopmaster.catalog_lang cl
    SET shortname = 'TEST' 
FROM shopmaster.lang l 
WHERE cl.langid = l.langid AND cl.catalogid = 7 AND l.code = 'fr';

In Postgres each reference to the table is separate. Your update is equivalent to this SELECT:

SELECT . . .
FROM shopmaster.catalog_lang CROSS JOIN
     shopmaster.catalog_lang cl LEFT JOIN
     shopmaster.lang l
     ON cl.langid = l.langid 
WHERE cl.catalogid = 7 AND l.code = 'fr';

And this is definitely not what you intend.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.