# 未知主键

**URL:** https://meta.discourse.org/t/unknown-primary-key/260985
**Category:** Self-hosting
**Created:** [2023年四月7日 21:02 UTC](https://meta.discourse.org/t/unknown-primary-key/260985 "2023-04-07T21:02:37Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![ibahnasy](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ibahnasy/32/301452_2.png) [@ibahnasy](https://meta.discourse.org/u/ibahnasy)
#### Post date: [2023年四月7日 21:02 UTC](https://meta.discourse.org/t/unknown-primary-key/260985/1 "2023-04-07T21:02:38Z")

</div>

登录时出现以下错误：

```plaintext
Started POST "/session" for <IP> at 2023-04-07 20:57:15 +0000
Processing by SessionController#create as */*
  Parameters: {"login"=>"<user>", "password"=>"[FILTERED]", "second_factor_method"=>"1", "timezone"=>"<TIME_ZONE>"}
start
done
Completed 500 Internal Server Error in 161ms (ActiveRecord: 0.0ms | Allocations: 154577)
ActiveRecord::UnknownPrimaryKey (Unknown primary key for table posts in model Post.)
lib/freedom_patches/ams_include_without_root.rb:49:in `include!'
app/controllers/application_controller.rb:496:in `serialize_data'
app/controllers/application_controller.rb:505:in `render_serialized'
app/controllers/session_controller.rb:714:in `login'
app/controllers/session_controller.rb:341:in `create'
app/controllers/application_controller.rb:397:in `block in with_resolved_locale'
app/controllers/application_controller.rb:397:in `with_resolved_locale'
lib/middleware/omniauth_bypass_middleware.rb:71:in `call'
lib/content_security_policy/middleware.rb:12:in `call'
lib/middleware/anonymous_cache.rb:358:in `call'
config/initializers/100-quiet_logger.rb:23:in `call'
config/initializers/100-silence_logger.rb:31:in `call'
lib/middleware/enforce_hostname.rb:23:in `call'
lib/middleware/request_tracker.rb:198:in `call'

```

另外，在尝试打开其他主题时，出现以下错误：

```plaintext
Started GET "/t/le-numerique-alternatif-et-ecologique-murena-e-os-fairphone-telecoop-commown/48638" for <IP> at 2023-04-07 20:56:40 +0000
Processing by TopicsController#show as HTML
  Parameters: {"slug"=>"le-numerique-alternatif-et-ecologique-murena-e-os-fairphone-telecoop-commown", "topic_id"=>"48638"}
Completed 500 Internal Server Error in 11ms (ActiveRecord: 0.0ms | Allocations: 3093)
ActiveRecord::UnknownPrimaryKey (Unknown primary key for table posts in model Post.)
lib/freedom_patches/fast_pluck.rb:59:in `pluck'
lib/freedom_patches/fast_pluck.rb:52:in `pluck'
lib/topic_view.rb:143:in `initialize'
app/controllers/topics_controller.rb:89:in `new'
app/controllers/topics_controller.rb:89:in `show'
app/controllers/application_controller.rb:397:in `block in with_resolved_locale'
app/controllers/application_controller.rb:397:in `with_resolved_locale'
lib/middleware/omniauth_bypass_middleware.rb:71:in `call'
lib/content_security_policy/middleware.rb:12:in `call'
lib/middleware/anonymous_cache.rb:356:in `call'
config/initializers/100-quiet_logger.rb:23:in `call'
config/initializers/100-silence_logger.rb:31:in `call'
lib/middleware/enforce_hostname.rb:23:in `call'
lib/middleware/request_tracker.rb:198:in `call'

```

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2023年四月8日 18:52 UTC](https://meta.discourse.org/t/unknown-primary-key/260985/2 "2023-04-08T18:52:58Z")

</div>

也许你的 Satan’s 没有被迁移？

你可以尝试

`rake db:migrate`

你没有设置跳过迁移吗？这是标准安装吗？

---

<div class="post-metadata">

### Author: ![ibahnasy](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ibahnasy/32/301452_2.png) [@ibahnasy](https://meta.discourse.org/u/ibahnasy)
#### Post date: [2023年四月9日 01:42 UTC](https://meta.discourse.org/t/unknown-primary-key/260985/3 "2023-04-09T01:42:00Z")

</div>

这是 docker 设置，Discourse 运行正常，我只是导出了 Docker 容器，并将其与 `/var/discourse` 一起导入到另一台服务器上，然后启动了它，就这样。  
我还执行了 `rake db:migrate`，但没有帮助。

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2023年四月9日 01:57 UTC](https://meta.discourse.org/t/unknown-primary-key/260985/4 "2023-04-09T01:57:39Z")

</div>

你恢复备份了吗？

你尝试过重建吗？

---

<div class="post-metadata">

### Author: ![MikeNolan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mikenolan/32/297597_2.png) [@MikeNolan](https://meta.discourse.org/u/MikeNolan)
#### Post date: [2023年四月9日 06:21 UTC](https://meta.discourse.org/t/unknown-primary-key/260985/5 "2023-04-09T06:21:44Z")

</div>

我不认为数据库文件在容器内，因此 PostgreSQL 可能会缺少 discourse 数据库，甚至可能缺少整个数据库文件集。

恢复应该可以解决问题，因为标准的 PostgreSQL 恢复应该会创建 discourse 数据库。（但如果失败，请尝试创建数据库然后进行恢复。）

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [2023年四月9日 15:17 UTC](https://meta.discourse.org/t/unknown-primary-key/260985/6 "2023-04-09T15:17:28Z")

</div>

> [@ibahnasy](#):
>
> 我只是导出了 Docker 容器，并将其与 `/var/discourse` 一起导入到另一台服务器上，然后启动了它，仅此而已。

这不是将 Discourse 实例移动到不同服务器的正确方法 [Move your Discourse Instance to a Different Server](https://meta.discourse.org/t/move-your-discourse-instance-to-a-different-server/15721?u=falco%EF%BC%8C%E8%80%8C%E4%B8%94%E6%B0%B8%E8%BF%9C%E4%B8%8D%E4%BC%9A%E5%A5%8F%E6%95%88%E3%80%82)
