【问题标题】:Rails app Foreign key error: Administrative user not allowed to delete other users inRails应用程序外键错误:不允许管理用户删除其他用户
【发布时间】:2015-03-24 07:56:49
【问题描述】:

在以管理用户身份登录时尝试删除用户时,我的 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"

我收到错误是因为用户 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

【问题讨论】:

  • 删除用户后,用户的汽车会发生什么情况?
  • 汽车也应该被删除,整个用户配置文件和相关汽车都被删除。
  • 在关联模型的用户模型中添加“dependent::destroy”。

标签: sql ruby-on-rails ruby heroku


【解决方案1】:

为那些不想删除相关记录的人添加替代方法(也许您想保留汽车记录以备后用)。

class User < ActiveRecord::Base
  ....
  has_many :cars, dependent: :nullify
end

关联的记录 foreign_key 将被设置为 nil。

对于像我这样解决您的问题的人可能很有用!

【讨论】:

  • 你节省了我的时间。
【解决方案2】:

为你的用户模型添加级联删除:

class User < ActiveRecord::Base
  ....
  has_many :cars, :dependent => :delete_all
end

【讨论】:

    【解决方案3】:

    创建迁移

    add_foreign_key :identities, :users, on_delete: :cascade

    为我工作。另见:https://stackoverflow.com/a/36137593/5521564

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-02
      • 1970-01-01
      相关资源
      最近更新 更多