I am trying to move bytea
data from one table to another, updating references in one query.
Therefore I would like to return data from the query used for the insert that is not used for the insert.
INSERT INTO file_data (data)
select image from task_log where image is not null
RETURNING id as file_data_id, task_log.id as task_log_id
But I get an error for that query:
[42P01] ERROR: missing FROM-clause entry for table "task_log"
I want to do something like:
WITH inserted AS (
INSERT INTO file_data (data)
SELECT image FROM task_log WHERE image IS NOT NULL
RETURNING id AS file_data_id, task_log.id AS task_log_id
)
UPDATE task_log
SET task_log.attachment_id = inserted.file_data_id,
task_log.attachment_type = 'INLINE_IMAGE'
FROM inserted
WHERE inserted.task_log_id = task_log.id;
But I fail to get all data used for the insert, I can't return the id from the subselect.
I was inspired by this answer on how to do that with Common Table Expressions but I can't find a way to make it work.