Sql Rails应用程序外键错误:不允许管理用户删除中的其他用户

Sql Rails应用程序外键错误:不允许管理用户删除中的其他用户,sql,ruby-on-rails,ruby,heroku,Sql,Ruby On Rails,Ruby,Heroku,当以管理用户身份登录时尝试删除用户时,我的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".

当以管理用户身份登录时尝试删除用户时,我的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”表中的用户
有很多
车,所以我认为这个错误不是因为删除了一个用户,而是因为删除了与该用户相关联的车。但我不知道如何修复我的数据库以便删除用户

以下是我的模式:

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

将级联删除添加到yout用户模型:

class User < ActiveRecord::Base
  ....
  has_many :cars, :dependent => :delete_all
end
class用户:全部删除
结束

为那些不想删除相关记录的人添加一个替代方案(也许你想保留cars记录供以后使用)

class用户
关联的记录外键将设置为nil


对于像我这样的人来说,你的问题是有用的

使用创建迁移

add\u-foreign\u-key:identies,:users,on\u-delete::cascade


对我有用。另请参见:

删除用户时,用户的汽车会发生什么情况?这些汽车也应该删除,整个用户配置文件和关联的汽车都会被删除。在关联模型的用户模型中添加“dependent::destroy”。您节省了我的时间。
class User < ActiveRecord::Base
  ....
  has_many :cars, dependent: :nullify
end