我正在做一个Ruby on Rails课程,我正在试图删除一个投资组合项目,但它给了我一个错误。这是错误。如何删除导轨中的项目?
ActiveRecord::InvalidForeignKey in PortfoliosController#destroy
PG::ForeignKeyViolation: ERROR: update or delete on table "portfolios" violates foreign key constraint "fk_rails_cc5ab4a1c3" on table "technologies" DETAIL: Key (id)=(12) is still referenced from table "technologies". : DELETE FROM "portfolios" WHERE "portfolios"."id" = $1
Extracted source (around line #52):
50
51 # Destroy/delete the record
52 @portfolio_item.destroy
53
54 # Redirect
55 respond_to do |format|`
Rails.root: /home/ubuntu/workspace/DevcampPortfolio/ClonedRepo/Devcamp-Portfolio
Application Trace | Framework Trace | Full Trace app/controllers/portfolios_controller.rb:52:in 'destroy'
Request
Parameters:
{"_method"=>"delete", "authenticity_token"=>"3fGuZqFrTblpS7aQGpfszjN24lSxu6SAr5/JRlLc0RDupDZ7BJKJr6kPnEYHa0/BR7rmTPnN+/i5Ptjb3wCLtw==", "id"=>"12"}
Response
Headers:
None
the code for the place I am in the course
我该如何改变以解决它?
我试图做你说的,但我无法弄清楚。你能告诉我什么代码与依赖:: destroy部分?我是新手,我不太了解。 –
在你的portfolio.rb文件中,写下“has_many technologies,dependent :: destroy”,当你销毁它时,它会杀死属于该组合的技术表中的元素 – Maxence
Postgres doesn'就像你杀了一位父母,并让孩子提及那位父母。位于技术领域的投资组合foreign_key则无所作为。我认为与其他数据库不同,你可以删除父对象,而不会引用它。 – Maxence