Cannot delete user from rails console

  • Used Zammad version: 3.4
  • Used Zammad installation source: package
  • Operating system: Ubuntu 18.04
  • Browser + version: Chrome 83

I installed Zammad and then I created my users (Agents) who logged in and tested the system (created organization, test tickets, etc).

Later, I added Google Authentication for logging in also (as we are G Suite customer - I restricted the Google Auth log into our domain only). I also asked our users to link their existing accounts with Google from Profile settings.

But before going live I discovered that the users are able to login with either Google Auth OR with existing local credentials. My aim is to allow my users to login ONLY with Google Auth, so I deleted all existing users of my Organization and asked them to sign up using Google directly. Before they signed up, I deleted all the relevant users using console with command:

User.find_by(email: ‘user@example.com’).destroy

Since Zammad has only been used for testing up till now, I have also deleted all tickets from console with command

Ticket.destroy_all

The only problem is one of the users is not getting deleted with this command. I get the error:
> ActiveRecord::InvalidForeignKey (PG::ForeignKeyViolation: ERROR: update or delete on table “users” violates foreign key constraint “fk_rails_edec76c076” on table “organizations”)

DETAIL: Key (id)=(15) is still referenced from table “organizations”.
: DELETE FROM “users” WHERE “users”.“id” = $1

I have followed all steps from this post here but still I get the same error.

Expected behavior:

  • Delete specific user

Actual behavior:

  • Cannot delete user

Steps to reproduce the behavior:

  • Not sure if this can be reproduced.

Thanks in Advance

Unassign the organization on the affected user and you’ll be fine.
In your case you could run User.update(organization_id: nil) to do that.

Thanks for the reply…

I already had tried removing from organization from GUI, but was still getting the same error, so I set it back.

I followed you steps and put in cli

User.find(15).update(organization_id: nil)

and after that User.find(15) showed that organization_id was nil. Again put in the same command as earlier - User.find(15).destroy , and got the same error again. I will put the full output below, it something to do with searchindex job.

irb(main):007:0> User.find(15).destroy
Group.find(1) notify TOUCH 2020-07-21 10:18:04 UTC
User.find(15) notify TOUCH 2020-07-21 10:18:04 UTC
Enqueued SearchIndexJob (Job ID: 2ee98e81-d95c-415f-9f0e-fc053ee49d2f) to DelayedJob(default) with arguments: “User”, 15
Group.find(6) notify TOUCH 2020-07-21 10:18:04 UTC
User.find(15) notify TOUCH 2020-07-21 10:18:04 UTC
Won’t enqueue SearchIndexJob (Job ID: d4095319-bda6-4f3d-81d0-913af077f3c1) because of already existing job with lock key ‘SearchIndexJob/User/15’.
Enqueued SearchIndexJob (Job ID: d4095319-bda6-4f3d-81d0-913af077f3c1) to DelayedJob(default) with arguments: “User”, 15
curl -X delete “http://localhost:9200/zammad_production_stats_store/_doc/3
200
Traceback (most recent call last):
1: from (irb):7
ActiveRecord::InvalidForeignKey (PG::ForeignKeyViolation: ERROR: update or delete on table “users” violates foreign key constraint “fk_rails_edec76c076” on table “organizations”)
DETAIL: Key (id)=(15) is still referenced from table “organizations”.
: DELETE FROM “users” WHERE “users”.“id” = $1

Thanks again.

Well I guess user with ID did create or update at least one organization then.

Organization.where(created_by_id: 15).update_all(created_by_id: 1)
Organization.where(updated_by_id: 15).update_all(updated_by_id: 1)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.