Loading production environment (Rails 6.1.7.3)
irb(main):001:0> Host::Base.where(id: [33]).destroy_all
Traceback (most recent call last):
2: from lib/tasks/console.rake:5:in `block in <top (required)>’
1: from (irb):1
ActiveRecord::InvalidForeignKey (PG::ForeignKeyViolation: ERROR: update or delete on table “hosts” violates foreign key constraint “fk_rails_a82ac722f1” on table “host_facets_reported_data_facets”)
DETAIL: Key (id)=(33) is still referenced from table “host_facets_reported_data_facets”.
irb(main):003:0> Host::Base.where(id: [33]).destroy_all
Traceback (most recent call last):
2: from lib/tasks/console.rake:5:in `block in <top (required)>’
1: from (irb):3
ActiveRecord::InvalidForeignKey (PG::ForeignKeyViolation: ERROR: update or delete on table “hosts” violates foreign key constraint “fk_rails_cadab260b9” on table “host_puppet_facets”)
DETAIL: Key (id)=(33) is still referenced from table “host_puppet_facets”.
Is there any reason you try to do this via foreman-rake?
foreman-rake is (as far as I understand) intended to be a developer and “last-resort” troubleshooting tool, not as a everyday production tool. I would recommend using either hammer, the API or the GUI to delete hosts. In my experience, neither of these cause any problems when deleting hosts (managed or not), as long as you don’t have any general problems in your database.
Hi - thanks for that - Yes I deleted the host using the foreman GUI.
And it doesn’t appear in any hammer commands. Such as “hammer host list”
But when I tried to “re-add” the new server and do anything with it in the GUI - it complains that the hostname is already in use.
So somehow the GUI delete left this entry behind, which is why I am trying to clean it up using the foreman-rake command, or any other method that works to be honest.
We have encountered similar problems in the past, in our case we could delete the “stale” host via GUI by setting the context to “Any Org”/“Any Loc” and then we could find the host and delete it via the regular GUI methods.
From our experience, this problem is probably related to Puppet or RHSM Fact uploads not being mapped to the correct host for some reason. Since we have encountered this behavior with hosts that were still in Foreman, too, effectively causing duplicate hosts, this is probably not related to host deletion but has been a prior problem that you just did not notice.
In our case, this stopped after we turned “Create new host when facts are uploaded” off in the settings, since we don’t need that feature.
I have exactly the same problem over here. I now at least know where the corruption in the DB is, but I cannot repair it due to the violated key constraint. I really need to solve this to get the hosts configurable again.
A funky workaround for some stuff is to just select the host from the host list and use a bulk operation (e.g. change environment). That succeeds although the basic operations on the host-specific GUI fail.
There it was solved by simply manually deleting the corresponding blocking data entities. So I did this as well and was then able to delete the host.
So first I identified the odd host:
sudo -u postgres psql foreman -c "SELECT id FROM hosts WHERE name = 'odd.host.name';"