当以管理用户身份登录并尝试删除用户时,我的 heroku 日志中出现以下错误:
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"
我收到错误是因为表“cars”中的用户 has_many
汽车,所以我认为错误不是删除用户而是与用户关联的汽车。但我不明白如何修复我的数据库以便删除用户。
这是我的模式:
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
最佳答案
为那些不想删除关联记录的人添加一个备选方案(也许您想保留汽车记录以备后用)。
class User < ActiveRecord::Base
....
has_many :cars, dependent: :nullify
end
关联的记录 foreign_key 将被设置为 nil。
对于像我这样的人回答您的问题很有用!
关于sql - Rails 应用程序外键错误 : Administrative user not allowed to delete other users in,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29227443/