购物车/结帐ActiveRecord :: InvalidForeignKey(PG :: ForeignKeyViolation:Heroku上的错误,但不是Localhost

时间:2017-05-19 21:24:56

标签: ruby-on-rails heroku

我的应用程序上有一个工作车/结帐功能,在localhost上工作得非常好,但是当我部署到Heroku项目时,当"添加到购物车"时,不会将其添加到购物车中。单击按钮。

Heroku日志显示错误:

2017-05-19T21:12:00.506230+00:00 app[web.1]: Started POST "/order_items" for 68.225.227.137 at 2017-05-19 21:12:00 +0000
2017-05-19T21:12:00.508991+00:00 app[web.1]: Processing by OrderItemsController#create as JS
2017-05-19T21:12:00.511223+00:00 app[web.1]: User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
2017-05-19T21:12:00.509041+00:00 app[web.1]: Parameters: {"utf8"=>"✓", "order_item"=>{"quantity"=>"1", "product_id"=>"2"}, "commit"=>"Add to Cart"}
2017-05-19T21:12:00.516450+00:00 app[web.1]: Product Load (0.7ms) SELECT "products".* FROM "products" WHERE "products"."active" = $1 AND "products"."id" = $2 LIMIT 1 [["active", "t"], ["id", 2]]
2017-05-19T21:12:00.513630+00:00 app[web.1]: (0.6ms) BEGIN
2017-05-19T21:12:00.531537+00:00 app[web.1]: SQL (12.8ms) INSERT INTO "orders" ("user_id", "subtotal", "created_at", "updated_at", "order_status_id") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["user_id", 1], ["subtotal", "499.0"], ["created_at", "2017-05-19 21:12:00.517193"], ["updated_at", "2017-05-19 21:12:00.517193"], ["order_status_id", 1]]
2017-05-19T21:12:00.532478+00:00 app[web.1]: (0.8ms) ROLLBACK
2017-05-19T21:12:00.533488+00:00 app[web.1]: 
2017-05-19T21:12:00.532671+00:00 app[web.1]: Completed 500 Internal Server Error in 24ms (ActiveRecord: 15.8ms)
2017-05-19T21:12:00.533491+00:00 app[web.1]: DETAIL: Key (order_status_id)=(1) is not present in table "order_statuses".
2017-05-19T21:12:00.533490+00:00 app[web.1]: ActiveRecord::InvalidForeignKey (PG::ForeignKeyViolation: ERROR: insert or update on table "orders" violates foreign key constraint "fk_rails_7a22cf8b0e"
2017-05-19T21:12:00.533492+00:00 app[web.1]: : INSERT INTO "orders" ("user_id", "subtotal", "created_at", "updated_at", "order_status_id") VALUES ($1, $2, $3, $4, $5) RETURNING "id"):
2017-05-19T21:12:00.533493+00:00 app[web.1]: app/controllers/order_items_controller.rb:7:in `create'
2017-05-19T21:12:00.533494+00:00 app[web.1]: 
2017-05-19T21:12:00.533495+00:00 app[web.1]: 
2017-05-19T21:12:06.465383+00:00 heroku[router]: at=info method=POST path="/order_items" host=aloop-offroad.herokuapp.com request_id=b13e7b04-7ac0-44fe-b58f-a850ce4cd7e8 fwd="68.225.227.137" dyno=web.1 connect=0ms service=16ms status=500 bytes=1754 protocol=http

谁能看到发生了什么?我迷失了,因为我无法看到任何适用于localhost而非Heroku的内容。

1 个答案:

答案 0 :(得分:1)

您的错误已经归结为,您尝试将记录插入orders,其值order_status_id违反约束条件。这可能是因为您使用一些OrderStatus对象在本地播种了数据库,并将其ID硬编码到您的代码中。但是,这些记录在您的生产数据库中不存在。您应该更新代码以根据请求从数据库中提取订单状态选项,然后在生产数据库中创建相关的状态记录。