Hi,
Looking for a guru to help solve a tricky problem... I am storing objects in one table, and their translations in another with something like the following schema:
There is always at least one translation (row) in nodes_t for each row in nodes. The first translation has src=1, the rest have src=0.
I want to retrieve the object and its translation, but if the translation for the specified language doesn't exist then I want the original or 'src' row.
For retrieving a single node I can do this by ORDERing by src and LIMITing to 1 row:
However how do I join multiple nodes with their translations, where the translation is the language requested (if it exists) or the src row otherwise? In psuedo code it is something like this:
I have looked at all kinds of combinations of INTERSECT, UNION, EXCEPT, EXISTS, IN etc and can't find a clean way. Ie, one that doesn't involve doing multiple queries. I think using DISTINCT ON(transof) would work but databases like SQLite don't support that.
Any ideas on how best to do this?
Cheers,
Mark.
Looking for a guru to help solve a tricky problem... I am storing objects in one table, and their translations in another with something like the following schema:
Code:
CREATE TABLE nodes (
id INTEGER NOT NULL PRIMARY KEY,
path VARCHAR(255) NOT NULL UNIQUE,
)
CREATE TABLE nodes_t (
id INTEGER NOT NULL PRIMARY KEY,
transof INTEGER NOT NULL REFERENCES nodes(id),
src INTEGER NOT NULL,
lang VARCHAR(8) NOT NULL,
title VARCHAR(255) NOT NULL,
There is always at least one translation (row) in nodes_t for each row in nodes. The first translation has src=1, the rest have src=0.
I want to retrieve the object and its translation, but if the translation for the specified language doesn't exist then I want the original or 'src' row.
For retrieving a single node I can do this by ORDERing by src and LIMITing to 1 row:
Code:
select
nodes.id,
nodes.path,
nodest.lang,
nodest.title
from nodes left join nodes_t
on nodes.id == nodest.transof
where
lang='de' or src=1 AND nodes.id=1
order by src
limit 1;
However how do I join multiple nodes with their translations, where the translation is the language requested (if it exists) or the src row otherwise? In psuedo code it is something like this:
Code:
id|transof|src|lang |title
1 |1 |1 |en |Home
2 |1 |0 |de |Hause
3 |2 |1 |en |About
Give me the translations for nodes 1 and 2:
if lang == 'de' then
return rows 2 and 3
else if lang == 'en' then
return rows 1 and 3
endif
I have looked at all kinds of combinations of INTERSECT, UNION, EXCEPT, EXISTS, IN etc and can't find a clean way. Ie, one that doesn't involve doing multiple queries. I think using DISTINCT ON(transof) would work but databases like SQLite don't support that.
Any ideas on how best to do this?
Cheers,
Mark.