# 更新 AI 插件失败

**URL:** https://meta.discourse.org/t/updating-ai-plugin-fails/315346
**Category:** Bug
**Tags:** ai, fixed
**Created:** [2024年七月7日 22:04 UTC](https://meta.discourse.org/t/updating-ai-plugin-fails/315346 "2024-07-07T22:04:52Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [2024年七月7日 22:04 UTC](https://meta.discourse.org/t/updating-ai-plugin-fails/315346/1 "2024-07-07T22:04:52Z")

</div>

从 [Latest update won't build due to various reasons](https://meta.discourse.org/t/latest-update-wont-build-due-to-ai-plugin/314875) 中提炼出此信息，因为该主题目前涉及三个不同的问题。

### 问题：

安装 AI 插件后尝试更新 Discourse 时，更新会产生以下错误：

```plaintext
PG::UndefinedColumn: ERROR: column llm_models.url does not exist
LINE 1: SELECT "llm_models".* FROM "llm_models" WHERE "llm_models"

```

这会导致 Discourse 无法使用，直到移除 AI 插件。

### 何时发生：

更新已安装 AI 插件的实例时  
从尚未应用迁移 `20240514171609` 的版本  
更新到提交 [8d5f901](https://github.com/discourse/discourse-ai/commit/8d5f901a6748d8a15fd227ef739f3c2f543fb376) 或更新的版本

（_我的主要问题是为什么只在更新已安装的插件时发生这种情况，而在新安装插件时不会发生。答案是，核心会捕获 `PG::UndefinedTable` 异常，但不会捕获 `PG:UndefinedColumn`。如果插件已安装，则异常是后者；如果插件是全新的，则异常是前者_）。

### 原因：

`plugin.rb` 调用 `LlmModel.enable_or_disable_srv_llm!`，该函数依赖于 `llm_models.url` 列的存在。但如果迁移 `20240514171609`（该迁移添加了该列）尚未应用，则会崩溃。  
问题在于 `db:migrate` _也_会运行此代码，这会在迁移应用之前导致 rake 任务崩溃。

### 建议的解决方法/修复方法：

1. 将代码更改为 `LlmModel.enable_or_disable_srv_llm! if Discourse.running_in_rack?`（这样 `db:migrate` 就不会调用它）  
或
2. 将代码更改为 `LlmModel.enable_or_disable_srv_llm! rescue nil`（这样就不会崩溃）  
或
3. 更改核心中的 [引导代码](https://github.com/discourse/discourse/blob/52a047a8beff58f3c9df57fd5b426e66636efd95/lib/plugin/instance.rb#L578-L583)，使其除了捕获 `PG::UndefinedTable` 外，还能捕获 `PG::UndefinedColumn`（这样就可以捕获此问题及未来类似的潜在问题）

@Roman

---

<div class="post-metadata">

### Author: ![Roman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/roman/32/157504_2.png) [@Roman](https://meta.discourse.org/u/Roman)
#### Post date: [2024年七月8日 12:59 UTC](https://meta.discourse.org/t/updating-ai-plugin-fails/315346/4 "2024-07-08T12:59:48Z")

</div>

感谢告知，@RGJ。我会处理此事，修复后会及时更新。

---

<div class="post-metadata">

### Author: ![Roman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/roman/32/157504_2.png) [@Roman](https://meta.discourse.org/u/Roman)
#### Post date: [2024年七月8日 22:28 UTC](https://meta.discourse.org/t/updating-ai-plugin-fails/315346/5 "2024-07-08T22:28:12Z")

</div>

已在此处推送修复程序：

> <https://github.com/discourse/discourse-ai/pull/708>
>
> \* Seeding the SRV-backed model should happen inside an initializer.
> \* Keep the …model up to date when the hidden setting changes.
> \* Use the correct Mixtral model name and fix previous data migration.
> \* URL validation should trigger only when we attempt to update it.
> 
> \[Context\](https://meta.discourse.org/t/updating-ai-plugin-fails/315346)

如果您仍然遇到问题，请告知我。

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [2024年七月8日 23:12 UTC](https://meta.discourse.org/t/updating-ai-plugin-fails/315346/6 "2024-07-08T23:12:01Z")

</div>

太棒了！谢谢你 🙂

---

<div class="post-metadata">

### Author: ![j.jaffeux](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j.jaffeux/32/60297_2.png) [@j.jaffeux](https://meta.discourse.org/u/j.jaffeux)
#### Post date: [2024年七月9日 07:27 UTC](https://meta.discourse.org/t/updating-ai-plugin-fails/315346/7 "2024-07-09T07:27:11Z")

</div>


