简体   繁体   English

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

[英]Cart/Checkout ActiveRecord::InvalidForeignKey (PG::ForeignKeyViolation: ERROR on Heroku but not Localhost

have a working cart/checkout functionality on my app which works perfectly fine on localhost, but when I deploy to Heroku items don't get added to the cart when the "Add to Cart" button is clicked. 在我的应用程序上具有可以正常工作的购物车/结帐功能,该功能在localhost上可以正常使用,但是当我部署到Heroku时,单击“添加到购物车”按钮时,商品不会添加到购物车中。

The Heroku logs show this for the error: 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

Can anyone see what's happening? 谁能看到发生了什么事? I'm lost because I can't see anything that would work on localhost but not Heroku . 我迷路了,因为我看不到任何可以在localhost上运行的东西,但看不到Heroku

Your error is, boiled down, that you're trying to insert a record into orders with a value for order_status_id that violates a constraint. 归根结底,您的错误是您试图将记录插入orders中的值为order_status_id的值违反约束的记录。 This is probably because you seeded your database locally with a few OrderStatus objects and hardcoded their ids into your code. 这可能是因为您使用几个OrderStatus对象在本地为数据库设置了种子,并将其ID硬编码到代码中。 Those records don't exist in your production database, though. 但是,这些记录在您的生产数据库中不存在。 You should probably update your code to pull the order status options from the database on request, and then create the relevant status records in your production database. 您可能应该更新代码以根据请求从数据库中提取订单状态选项,然后在生产数据库中创建相关的状态记录。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Accepts_nested_attributes_for引发ActiveRecord :: InvalidForeignKey:PG :: ForeignKeyViolation:ERROR - Accepts_nested_attributes_for raises ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR ActiveRecord :: InvalidForeignKey:PG :: ForeignKeyViolation:错误:对表进行更新或删除违反了外键约束 - ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: update or delete on table violates foreign key constraint Rails 中的自定义名称外键导致“ActiveRecord::InvalidForeignKey:PG::ForeignKeyViolation”错误 - Custom name foreign key in Rails causing 'ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation' error Minitest:关联AR模型的装置导致ActiveRecord :: InvalidForeignKey:运行测试时PG :: ForeignKeyViolation - Minitest: fixtures for associated AR models results in ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation when tests are run belongs_to 的 Rails 5 问题,当关联 model 具有自定义表名称时。ActiveRecord::InvalidForeignKey 异常:PG::ForeignKeyViolation: - Rails 5 trouble with belongs_to, when association model have custom table name.ActiveRecord::InvalidForeignKey Exception:PG::ForeignKeyViolation: ActiveRecord :: InvalidForeignKey错误 - ActiveRecord::InvalidForeignKey Error Rails PG::ForeignKeyViolation:错误:在表上插入或更新 - Rails PG::ForeignKeyViolation: ERROR: insert or update on table 的ActiveRecord :: InvalidForeignKey - ActiveRecord::InvalidForeignKey PG::ForeignKeyViolation:错误:在表“...”上插入或更新违反外键约束 - PG::ForeignKeyViolation: ERROR: insert or update on table “…” violates foreign key constraint Heroku-ActiveRecord :: StatementInvalid(PG :: Error:ERROR:列“ requested”不存在 - Heroku - ActiveRecord::StatementInvalid (PG::Error: ERROR: column “requested” does not exist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM