PG::ForeignKeyViolation: ERROR: update or delete on table

PG::ForeignKeyViolation: ERROR: update or delete on table

PG::ForeignKeyViolation: ERROR:  update or delete on table "users" violates foreign key constraint "fk_rails_c98ef61810" on table "licences"
DETAIL:  Key (id)=(7) is still referenced from table "licences".

我已经有 belongs_to :approved_by_admin, class_name: 'Admin', optional: true

但是为什么仍然会出现这个错误?

    add_reference :licences, :approved_by_admin, foreign_key: { to_table: :users }

好的,用户希望在删除用户时将许可证中的引用设置为空,但不想触及架构。我认为 has_many 管理员协会(应用程序级别)的 dependent: :nullify 可能是解决方案

# admin
has_many :dont_know_the_association, dependent: :nullify