ga-wdi-boston / full-stack-project

Other
8 stars 64 forks source link

Delete is now failing after changing a my meals serializer #296

Closed louarnos closed 8 years ago

louarnos commented 8 years ago

I think this stems back to the solution that @RDegnen and I came up with for my Post issue. When I delete my server gives me:

tarted DELETE "/meals/45" for ::1 at 2016-05-03 13:55:02 -0400
Processing by MealsController#destroy as */*
  Parameters: {"id"=>"45"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."token" = $1 LIMIT 1  [["token", "69b280e59585645e9f16649119d711fc"]]
  Meal Load (0.2ms)  SELECT  "meals".* FROM "meals" WHERE "meals"."user_id" = $1 AND "meals"."id" = $2 LIMIT 1  [["user_id", 4], ["id", 45]]
   (0.2ms)  BEGIN
  SQL (41.2ms)  DELETE FROM "meals" WHERE "meals"."id" = $1  [["id", 45]]
   (0.2ms)  ROLLBACK
Completed 500 Internal Server Error in 46ms (ActiveRecord: 42.0ms)

ActiveRecord::InvalidForeignKey (PG::ForeignKeyViolation: ERROR:  update or delete on table "meals" violates foreign key constraint "fk_rails_c04a4407b1" on table "meal_items"
DETAIL:  Key (id)=(45) is still referenced from table "meal_items".
: DELETE FROM "meals" WHERE "meals"."id" = $1):
  app/controllers/meals_controller.rb:46:in `destroy'

  Rendered /usr/local/var/rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_source.erb (7.0ms)
  Rendered /usr/local/var/rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.9ms)
  Rendered /usr/local/var/rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)
  Rendered /usr/local/var/rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (83.0ms)

If I'm understanding it correctly, because a reference to the meal that is being deleted still exists on the meal_items table, it fails.

RealWeeks commented 8 years ago

Maybe @RDegnen can help since he worked with you on it. Did you add a dependent destroy?

louarnos commented 8 years ago

No we didnt. I dont really wanna solve this until my other issue is solved because they seem dependent on eachother

GA-MEB commented 8 years ago

@louarnos The problem is that there isn't a dependent destroy. You're trying to delete a Meal, but that Meal is being referenced by a MealItem. Deleting the Meal would make that MealItem's foreign key point to a non-existent meal, violating the not-null constraint. ActiveRecord won't let you do that.

GA-MEB commented 8 years ago

You need to either (a) set up dependent_destroy on your Meals (and, presumably, Foods), or (b) manually delete any related MealItems any time you delete a Meal or Food.

RealWeeks commented 8 years ago

@MattB-GA-Boston are you addressing this along with #292 as well?

RealWeeks commented 8 years ago

@louarnos how is this going?

louarnos commented 8 years ago

@J-Weeks I'm working on it... I put a dependent destroy in my meals serializer and its still not working. I'm not convinced my serializers are set-up totally correctly.

RDegnen commented 8 years ago

the depended destroy would go in your model, not your serializer

louarnos commented 8 years ago

Needed to put a dependent destroy in my model, not my serializer...