# 上次更新期间 DB 迁移失败

**URL:** <https://meta.discourse.org/t/db-migration-failure-during-the-last-update/119461>\
**Category:** Bug\
**Created:** [2019年六月4日 08:48 UTC](https://meta.discourse.org/t/db-migration-failure-during-the-last-update/119461 "2019-06-04T08:48:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![axil](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/axil/32/81682_2.png) [@axil](https://meta.discourse.org/u/axil)\
**Post date:** [2019年六月4日 08:48 UTC](https://meta.discourse.org/t/db-migration-failure-during-the-last-update/119461/1 "2019-06-04T08:48:32Z")

</div>

I tried to update our forum via the web UI (`tests-passed` branch) and Activerecord failed due to a DB migration error. There wasn’t a way to rollback, the button didn’t show up.

The error in the Rails console was:

```plaintext
: SELECT "javascript_caches".* FROM "javascript_caches" WHERE "javascript_caches"."theme_id" = 2)
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-mini-profiler-1.0.2/lib/patches/db/pg.rb:69:in `async_exec_params'
Failed to handle exception in exception app middleware : PG::UndefinedColumn: ERROR: column javascript_caches.theme_id does not exist

```

The migration was failing because of a foreign key violation:

```plaintext
DETAIL: Key (theme_field_id)=(6) is not present in table "theme_fields".                                                    
: ALTER TABLE "javascript_caches" ADD CONSTRAINT "fk_rails_ed33506dbd"                                                       
FOREIGN KEY ("theme_field_id")                                                                                               
  REFERENCES "theme_fields" ("id")                                                                                           
 ON DELETE CASCADE  

```

We had to remove the offending row from `javascript_caches` for the migration to succeed:

```plaintext
  5 | 6 | da39a3ee5e6b4b0d3255bfef95601890afd80709 | | 2018-10-18 10:03:26.069247 | 2018-10-18 10:03:26.069247

```

Our current version is [https://github.com/discourse/discourse/compare/22abad415192b53b899ea6a98b8c7f73c85ca0c1...tests-passed](https://github.com/discourse/discourse/compare/22abad415192b53b899ea6a98b8c7f73c85ca0c1...tests-passed)

Our issue in GitLab [GitLab forum error 500 (#6844) · Issues · GitLab.com / GitLab Infrastructure Team / Production Engineering · GitLab](https://gitlab.com/gitlab-com/gl-infra/infrastructure/issues/6844).

---

<div class="post-metadata">

**Author:** ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)\
**Post date:** [2019年六月4日 09:43 UTC](https://meta.discourse.org/t/db-migration-failure-during-the-last-update/119461/2 "2019-06-04T09:43:01Z")

</div>

@david just fixed this, can you rebuild again?

The migration is atomic so there is no permanent damage or half rolled out migrations like you can get in MySQL

---

<div class="post-metadata">

**Author:** ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)\
**Post date:** [2019年六月4日 21:52 UTC](https://meta.discourse.org/t/db-migration-failure-during-the-last-update/119461/3 "2019-06-04T21:52:04Z")

</div>


