When trying to delete a user when logged in as the administrative user, I get the following error in my heroku logs:
2015-03-24T07:47:23.506661+00:00 app[web.1]: Started DELETE "/users/1" for 128.252.25.47 at 2015-03-24 07:47:23 +0000
2015-03-24T07:47:23.534256+00:00 app[web.1]: SQL (4.4ms) DELETE FROM "users" WHERE "users"."id" = $1 [["id", 1]]
2015-03-24T07:47:23.517508+00:00 app[web.1]: User Load (1.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
2015-03-24T07:47:23.541747+00:00 app[web.1]: Completed 500 Internal Server Error in 31ms
2015-03-24T07:47:23.534529+00:00 app[web.1]: PG::ForeignKeyViolation: ERROR: update or delete on table "users" violates foreign key constraint "fk_rails_274c57dd65" on table "cars"
2015-03-24T07:47:23.534534+00:00 app[web.1]: : DELETE FROM "users" WHERE "users"."id" = $1
2015-03-24T07:47:23.544385+00:00 app[web.1]: : DELETE FROM "users" WHERE "users"."id" = $1):
2015-03-24T07:47:23.544388+00:00 app[web.1]:
2015-03-24T07:47:23.544384+00:00 app[web.1]: DETAIL: Key (id)=(1) is still referenced from table "cars".
2015-03-24T07:47:23.544387+00:00 app[web.1]: app/controllers/users_controller.rb:45:in `destroy'
2015-03-24T07:47:23.544390+00:00 app[web.1]:
2015-03-24T07:47:23.536639+00:00 app[web.1]: (1.7ms) ROLLBACK
2015-03-24T07:47:23.544377+00:00 app[web.1]:
2015-03-24T07:47:23.510694+00:00 app[web.1]: Processing by UsersController#destroy as HTML
2015-03-24T07:47:23.534532+00:00 app[web.1]: DETAIL: Key (id)=(1) is still referenced from table "cars".
2015-03-24T07:47:23.510838+00:00 app[web.1]: Parameters: {"authenticity_token"=>"iKb5lOUvx8qDoGHtVWNgYRL/dIk5zWFU5kiQpWtGOnabA+D7Yg1gj86NHwYKMmFeK5hh4F/3jndyKHHDHApVoQ==", "id"=>"1"}
2015-03-24T07:47:23.515104+00:00 app[web.1]: User Load (2.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 2]]
2015-03-24T07:47:23.544382+00:00 app[web.1]: ActiveRecord::InvalidForeignKey (PG::ForeignKeyViolation: ERROR: update or delete on table "users" violates foreign key constraint "fk_rails_274c57dd65" on table "cars"
I'm getting the error because of the table "cars" where a user has_many
cars, so I think the error isn't with deleting a user but the cars associated with the user. But I don't understand how to fix my database so a user can be deleted.
Here are my schemas:
schema.rb
create_table "cars", force: :cascade do |t|
t.text "year"
t.text "brand"
t.text "model"
t.text "vin"
t.text "mileage"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "picture"
end
add_index "cars", ["user_id"], name: "index_cars_on_user_id"
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.string "phone"
t.string "remember_digest"
t.boolean "admin", default: false
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["phone"], name: "index_users_on_phone", unique: true