I've already read this, this and this, but I cant make this SQL work:
INSERT INTO main_phrase (description) VALUES ('Mot commun féminin pluriel animaux');
/* ERROR: */
WITH
t1 AS (
SELECT id
FROM main_phrase
WHERE description='Mot commun féminin pluriel animaux'
),
t2 AS (
SELECT id
FROM main_groupecategories
WHERE description='Mot commun féminin pluriel animaux'
)
INSERT
INTO main_phrasegroupecategories (phrase_id, groupe_categories_id)
VALUES (t1.id, t2.id);
I get:
ERROR: missing entry for the clause FROM for table t1
What am I missing?
main_phrase
to the row(s) in main_groupecategories
with the same description
.main_phrase.id
is a serial
column.You cannot refer to any tables (including CTE) in a free-standing VALUES
expression, you would have to use SELECT
with a FROM
clause. But there is a better solution. See below.
Use a data-modifying CTE instead to make the whole operation shorter, safer and faster:
WITH p AS (
INSERT INTO main_phrase (description)
VALUES ('Mot commun féminin pluriel animaux') -- provide description once
RETURNING id, description -- and reuse it further down
)
INSERT INTO main_phrasegroupecategories (phrase_id, groupe_categories_id)
SELECT p.id, g.id
FROM p
JOIN main_groupecategories g USING (description);
If you want to use any values of the new rows, have them returned immediately with another RETURNING
clause to the second INSERT
.
Why would you have the same description
redundantly in both tables of your (presumed) many-to-many relationship? Might be a problem in your database design.
Related: