我在开发和测试中使用sqlite并部署到在生产中使用Postgres的Heroku。我为'评论'创建了一个新模型。通过在终端中运行以下命令:
rails g model Review comment:text star:integer room:references reservation:references guest:references host:references type
rails g model GuestReview --parent=Review --migration=false
rails g model HostReview --parent=Review --migration=false
rails db:migrate
这是上述命令的输出:
$ rails g model Review comment:text star:integer room:references reservation:references guest:references host:references type
Running via Spring preloader in process 6862
invoke active_record
create db/migrate/20180613031058_create_reviews.rb
create app/models/review.rb
invoke test_unit
create test/models/review_test.rb
create test/fixtures/reviews.yml
$ rails g model GuestReview --parent=Review --migration=false
Running via Spring preloader in process 6872
invoke active_record
create app/models/guest_review.rb
invoke test_unit
create test/models/guest_review_test.rb
create test/fixtures/guest_reviews.yml
$ rails g model HostReview --parent=Review --migration=false
Running via Spring preloader in process 6880
invoke active_record
create app/models/host_review.rb
invoke test_unit
create test/models/host_review_test.rb
create test/fixtures/host_reviews.yml
$ rails db:migrate
== 20180613031058 CreateReviews: migrating
====================================
-- create_table(:reviews)
-> 0.0806s
== 20180613031058 CreateReviews: migrated (0.0828s)
===========================
该网站通常在localhost上开发,我可以在检查sqlite中的数据时正确保存评论。当我部署到Heroku并运行
时heroku run rake db:migrate
我收到以下错误:
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: relation "guests" does not exist
: CREATE TABLE "reviews" ("id" serial primary key, "comment" text,
"star" integer DEFAULT 1, "room_id" integer, "reservation_id" integer,
"guest_id" integer, "host_id" integer, "type" character varying,
"created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL,
CONSTRAINT "fk_rails_b98b7304fe"
FOREIGN KEY ("room_id")
REFERENCES "rooms" ("id")
, CONSTRAINT "fk_rails_628a43e51f"
FOREIGN KEY ("reservation_id")
REFERENCES "reservations" ("id")
, CONSTRAINT "fk_rails_eaa1ce2871"
FOREIGN KEY ("guest_id")
REFERENCES "guests" ("id")
, CONSTRAINT "fk_rails_ac5cb4d097"
FOREIGN KEY ("host_id")
REFERENCES "hosts" ("id")
)
以下是评论的迁移文件:
class CreateReviews < ActiveRecord::Migration[5.0]
def change
create_table :reviews do |t|
t.text :comment
t.integer :star, default: 1
t.references :room, foreign_key: true
t.references :reservation, foreign_key: true
t.references :guest, foreign_key: true
t.references :host, foreign_key: true
t.string :type
t.timestamps
end
end
end
以下是评论模型:
class Review < ApplicationRecord
end
以下是guest_review模型:
class GuestReview < Review
belongs_to :guest, class_name: "User"
end
这是host_review模型:
class HostReview < Review
belongs_to :host, class_name: "User"
end
似乎在Heroku Postgres上它正在寻找一个不存在的宾客桌。有没有办法解决此错误,以便它在我的开发环境和Heroku中正常运行?
答案 0 :(得分:2)
在迁移中,t.references :guest, foreign_key: true
执行两项操作:
bigint
的整数(或guest_id
,具体取决于您的Rails版本)。guest_id
列到id
表中的guests
列。但您尝试引用的表格是users
(即User
模型),而不是guests
。由于名称不匹配,您必须调整t.references
调用以告诉ActiveRecord引用哪个表:
t.references :guest, foreign_key: { to_table: :users }
同样适用于无法推断表名的任何其他t.references
调用。
一旦解决了您的问题,您应该在开发环境中安装PostgreSQL,以便在同一堆栈上进行开发,测试和部署。 ActiveRecord不提供超出最简单和最简单的事情的数据库可移植性,因此使用差异数据库进行开发和部署是灾难的一种方法。