I am basically trying to achieve what would be the equivalent of this c#
in postgres pretending that tables are some lists
List<Comment> comments;
List<Post> posts;
//SELECT
foreach (var comment in comments.OrderBy(c => c.id))
{
//INSERT
var post = new Post(title: comment.title);
posts.Add(post);
//UPDATE
comment.postId = post.id;
}
Basically, I am adding a new "parent" table and I need to run some code in the migration so every existing "child" has one parent. My postgres skills are limited so I don't know where to start.
Here is what I have so far
DO
$do$
BEGIN
FOR comment IN
SELECT * FROM comments ORDER BY id
LOOP
INSERT INTO posts (title)
VALUES (comment.title);
-- how do I update???
END LOOP;
END
$do$;