【发布时间】:2016-01-19 23:33:14
【问题描述】:
我正在将一个工作应用程序(在本地开发)推送到 Heroku 环境。当尝试在生产环境中运行 db:migrate 时,迁移失败,因为 postgreSQL 似乎正在做一些我在 localhost 上的 sqlite 没有做的健全性检查,例如:
我为帖子模型创建了一个迁移,并且已经定义了与(此时)不存在的评论模型的关系。 Postgre 抱怨表注释不存在。 (通过重新安排迁移解决了这个问题,我不知道)
我在我的模型中使用了一种 STI 技巧,因为模型练习、视频和课程都将它们的数据存储在同一个表(步骤)中。练习有很多问题,但在尝试创建表格时,Postgre 抱怨缺少表格练习。
有没有办法修复第二个错误,或者有没有一种通用的方法可以防止 rails 受到健全性检查(相关表是否存在)?
编辑:
好的,所以相关的迁移是这个:
class CreateQuestions < ActiveRecord::Migration
def change
create_table :questions do |t|
t.string :title
t.references :exercise, index: true, foreign_key: true
t.text :content
t.timestamps null: false
end
end
end
问题在于没有表exercises,因为该表名为steps。因此,PostgreSQL 的参照完整性被破坏,导致以下错误:
== 20151208132820 CreateQuestions: migrating ==================================
-- create_table(:questions)
(14.1ms) CREATE TABLE "questions" ("id" serial primary key, "title" character varying, "exercise_id" integer, "content" text, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL)
(5.2ms) CREATE INDEX "index_questions_on_exercise_id" ON "questions" ("exercise_id")
(9.3ms) ALTER TABLE "questions" ADD CONSTRAINT "fk_rails_5ba13b3a6e"
FOREIGN KEY ("exercise_id")
REFERENCES "exercises" ("id")
PG::UndefinedTable: ERROR: relation "exercises" does not exist
: ALTER TABLE "questions" ADD CONSTRAINT "fk_rails_5ba13b3a6e"
FOREIGN KEY ("exercise_id")
REFERENCES "exercises" ("id")
【问题讨论】:
-
部署后是否尝试重启 Heroku?
-
呃,不是真的……有必要吗?我该怎么做?
-
粘贴与问题相关的迁移和模型。
-
贴上相关代码
标签: ruby-on-rails postgresql heroku