# 修复固定链接，使其指向主题URL而不是主题的第一个帖子

**URL:** <https://meta.discourse.org/t/fix-permalinks-to-point-to-topic-url-and-not-to-first-post-of-topic/218262>\
**Category:** Migration\
**Created:** [2022年二月15日 16:38 UTC](https://meta.discourse.org/t/fix-permalinks-to-point-to-topic-url-and-not-to-first-post-of-topic/218262 "2022-02-15T16:38:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rrit](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@rrit](https://meta.discourse.org/u/rrit)\
**Post date:** [2022年二月15日 16:38 UTC](https://meta.discourse.org/t/fix-permalinks-to-point-to-topic-url-and-not-to-first-post-of-topic/218262/1 "2022-02-15T16:38:35Z")

</div>

在自定义迁移后，许多永久链接指向了主题的第一篇帖子，而不是主题 URL 本身。

例如，永久链接指向  
`https://forum.example.com/t/example-topic/123/1`

而不是  
`https://forum.example.com/t/example-topic/123`

* * *

### 检查

如果以下 SQL 查询有记录，则表示 Discourse 论坛受到影响：

```sql
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;

```

### 快速修复

如果永久链接最初指向主题的第一篇帖子，此操作会将永久链接更改为直接指向主题。

```sql
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;

```

这可能是一个在迁移脚本末尾运行的良好清理例程。

---

<div class="post-metadata">

**Author:** ![cocococosti](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cocococosti/32/231474_2.png) [@cocococosti](https://meta.discourse.org/u/cocococosti)\
**Post date:** [2022年二月15日 18:06 UTC](https://meta.discourse.org/t/fix-permalinks-to-point-to-topic-url-and-not-to-first-post-of-topic/218262/2 "2022-02-15T18:06:56Z")

</div>

> [@rrit](#):
>
> 在自定义迁移后，许多永久链接都指向了主题中的第一篇文章，而不是主题本身的 URL。

您是如何创建永久链接的？

例如，在 [discourse/script/import\_scripts/vanilla\_mysql.rb at main · discourse/discourse · GitHub](https://github.com/discourse/discourse/blob/main/script/import_scripts/vanilla_mysql.rb#L545) 这里，他们专门检查文章编号，如果文章编号是 1，那么文章编号就不会被传递（保持为 NULL），因此永久链接不应该指向第一篇文章。

---

<div class="post-metadata">

**Author:** ![rrit](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@rrit](https://meta.discourse.org/u/rrit)\
**Post date:** [2022年二月15日 20:47 UTC](https://meta.discourse.org/t/fix-permalinks-to-point-to-topic-url-and-not-to-first-post-of-topic/218262/3 "2022-02-15T20:47:47Z")

</div>

我采用了一种基于 SQL 的方法来处理永久链接，该方法基于与以下代码类似的代码：

> <https://github.com/discourse/discourse/blob/c5fd8c42dbf82dde6734353acb3578e90ecdcb58/script/import_scripts/lithium.rb#L831>

… 但缺少 `AND post_number <> 1`——所以这就是罪魁祸首。

---

<div class="post-metadata">

**Author:** ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)\
**Post date:** [2022年三月17日 20:47 UTC](https://meta.discourse.org/t/fix-permalinks-to-point-to-topic-url-and-not-to-first-post-of-topic/218262/4 "2022-03-17T20:47:50Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
