After a customized migration many permalinks were pointing to the first post in a topic instead of the topic-url itself.
e.g. permalinks pointing to https://forum.example.com/t/example-topic/123/1
instead of https://forum.example.com/t/example-topic/123
Check
A Discourse forum is affected if there are records for this SQL query:
SELECT permalinks.*,
posts.id,
posts.topic_id,
posts.post_number
FROM permalinks
LEFT JOIN posts
ON posts.id = permalinks.post_id
WHERE permalinks.post_id IS NOT NULL
AND posts.post_number = 1
LIMIT 20;
Quick Fix
This changes permalinks to directly point to the topic, if they were originally pointing to the first post in topic.
UPDATE permalinks
SET topic_id = posts.topic_id,
post_id = NULL
FROM posts
WHERE permalinks.post_id = posts.id
AND permalinks.post_id IS NOT NULL
AND posts.post_number = 1;
This might be a good cleanup routine to run at the end of migration scripts.