Bug 1197477 - ERROR: update or delete on table "hosts" violates foreign key constraint "tokens_host_id_fk" on table "tokens"
Summary: ERROR: update or delete on table "hosts" violates foreign key constraint "to...
Keywords:
Status: CLOSED ERRATA
Alias: None
Product: Red Hat Satellite
Classification: Red Hat
Component: Discovery Plugin
Version: Unspecified
Hardware: Unspecified
OS: Unspecified
unspecified
high
Target Milestone: Unspecified
Assignee: orabin
QA Contact: Sachin Ghai
URL: http://projects.theforeman.org/issues...
Whiteboard:
Depends On:
Blocks: 1210847
TreeView+ depends on / blocked
 
Reported: 2015-03-01 16:33 UTC by orabin
Modified: 2019-11-14 06:38 UTC (History)
9 users (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2015-08-12 05:28:14 UTC
Target Upstream Version:
Embargoed:


Attachments (Terms of Use)
UI throwing error in deleting a discovered host (62.64 KB, image/png)
2015-03-16 12:15 UTC, Sachin Ghai
no flags Details
Foreign_key issue still reproducible with snap8 on deleting a discovered host (132.76 KB, image/png)
2015-03-31 13:09 UTC, Sachin Ghai
no flags Details
production.log (11.49 KB, text/plain)
2015-03-31 13:11 UTC, Sachin Ghai
no flags Details
reproducible with GA snap2 (37.52 KB, image/png)
2015-05-04 12:24 UTC, Sachin Ghai
no flags Details


Links
System ID Private Priority Status Summary Last Updated
Foreman Issue Tracker 9597 0 None None None 2016-04-22 16:37:05 UTC
Red Hat Product Errata RHSA-2015:1592 0 normal SHIPPED_LIVE Important: Red Hat Satellite 6.1.1 on RHEL 6 2015-08-12 09:04:35 UTC

Description orabin 2015-03-01 16:33:16 UTC
Description of problem:
FK violation when trying to delete a discovered host after provision

Version-Release number of selected component (if applicable): discovery 2.0

How reproducible:
When provision on a discovered host fails there is a token created that is not deleted.
Provisioning the same host again will create a new token for that host.
Trying to delete the host after provision deletes only the second token,
to avoid leaving an orphaned token the delete fails with a FK violation error

Steps to Reproduce:
1. discover a host
2. try to provision that host but don't put anything in the host edit form
3. save
4. fill in correct values to provision the host
5. save
6. try to delete the new managed host that was created

Actual results: The error in the title


Expected results:  host is deleted


Additional info:

Comment 1 RHEL Program Management 2015-03-01 23:21:50 UTC
Since this issue was entered in Red Hat Bugzilla, the release flag has been
set to ? to ensure that it is properly evaluated for this release.

Comment 3 Bryan Kearney 2015-03-09 14:05:35 UTC
Moving to POST since upstream bug http://projects.theforeman.org/issues/9597 has been closed
-------------
Ori Rabin
Applied in changeset commit:foreman_discovery|bc01733928b6babaf0feefbbce326169ae588b08.

Comment 6 Sachin Ghai 2015-03-13 17:44:40 UTC
Verified with:

sat6.1 beta snap6 compose2 (Satellite-6.1.0-RHEL-6-20150311.1).

I followed all the steps in bz description, but could n't reproduce the reported the issue. Moving to verified.

Comment 7 Sachin Ghai 2015-03-16 12:10:59 UTC
I can reproduce same issue but in a bit different scenario:

I re-discovered same host which was discovered earlier  and this throws following error:

--
2015-03-16 12:09:21 [I] Processing by Api::V2::DiscoveredHostsController#facts as JSON
2015-03-16 12:09:21 [I]   Parameters: {"facts"=>"[FILTERED]", "apiv"=>"v2", "discovered_host"=>{"facts"=>"[FILTERED]"}}
2015-03-16 12:09:21 [I] Import facts for 'mac525400a5bc0e' completed. Added: 0, Updated: 4, Deleted 0 facts
2015-03-16 12:09:22 [I] Match found for host mac525400a5bc0e (11) rule rule1 (1)
2015-03-16 12:09:22 [I] ForemanDiscovery: Rebooting mac525400a5bc0e
2015-03-16 12:09:22 [I] ForemanDiscovery: reboot result: successful
2015-03-16 12:09:22 [W] Rolling back due to a problem: []
2015-03-16 12:09:22 [E] ActiveRecord::Rollback
2015-03-16 12:09:22 [E] Unprocessable entity Host::Discovered (id: 11):
  Name has already been taken

---

but when I tried to delete it from webUI, I got following error:

--
2015-03-16 12:08:34 [I] Processing by DiscoveredHostsController#destroy as HTML
2015-03-16 12:08:34 [I]   Parameters: {"authenticity_token"=>"5cZuH5IlnPJKmdqlaE3t47OH4JcWmXbzeenN6It/fjo=", "id"=>"mac525400a5bc0e"}
2015-03-16 12:08:34 [W] Operation FAILED: ERROR:  update or delete on table "hosts" violates foreign key constraint "tokens_host_id_fk" on table "tokens"
DETAIL:  Key (id)=(11) is still referenced from table "tokens".

2015-03-16 12:08:34 [I]   Rendered common/500.html.erb within layouts/application (6.0ms)
2015-03-16 12:08:34 [I]   Rendered layouts/base.html.erb (1.4ms)
2015-03-16 12:08:34 [I] Completed 500 Internal Server Error in 84ms (Views: 11.4ms | ActiveRecord: 16.1ms)
---

Comment 8 Sachin Ghai 2015-03-16 12:11:52 UTC
Reproducible with sat6.1 beta snap6 compose2 (Satellite-6.1.0-RHEL-6-20150311.1).

Comment 9 Sachin Ghai 2015-03-16 12:15:10 UTC
Created attachment 1002240 [details]
UI throwing error in deleting a discovered host

Comment 13 Lukas Zapletal 2015-03-24 08:01:17 UTC
Oh, thanks. Re-test please.

Comment 14 Sachin Ghai 2015-03-26 14:25:29 UTC
I reproduced similar issue on Sat6.1 beta snap7 compose3..

Comment 16 Sachin Ghai 2015-03-26 14:29:57 UTC
I'll test this again with snap8 and will update here.. thanks

Comment 17 Sachin Ghai 2015-03-31 13:08:59 UTC
Guys, this issue is reproducible in snap8 (Satellite-6.1.0-RHEL-7-20150324.0).

--
2015-03-31 18:36:37 [I] Processing by HostsController#destroy as HTML
2015-03-31 18:36:37 [I]   Parameters: {"authenticity_token"=>"x/5flBIFQMT2FhwJCPof+IJ+JZlEPZF8cnJQo4Cn0tw=", "id"=>"mac525400492906.lab.eng.pnq.redhat.com"}
2015-03-31 18:36:37 [E] PGError: ERROR:  update or delete on table "hosts" violates foreign key constraint "tokens_host_id_fk" on table "tokens"
DETAIL:  Key (id)=(21) is still referenced from table "tokens".
: DELETE FROM "hosts" WHERE "hosts"."type" IN ('Host::Managed') AND "hosts"."id" = $1 (ActiveRecord::InvalidForeignKey)
/opt/rh/ruby193/root/usr/share/gems/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:1171:in `get_last_result'
/opt/rh/ruby193/root/usr/share/gems/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:1171:in `exec_cache'
/opt/rh/ruby193/root/usr/share/gems/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:676:in `block in exec_delete'
/opt/rh/ruby193/root/usr/share/gems/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
/opt/rh/ruby193/root/usr/share/gems/gems/activesupport-3.2.8/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/opt/rh/ruby193/root/usr/share/gems/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
--

Comment 18 Sachin Ghai 2015-03-31 13:09:48 UTC
Created attachment 1009040 [details]
Foreign_key issue still reproducible with snap8 on deleting a discovered host

Comment 19 Sachin Ghai 2015-03-31 13:11:12 UTC
Created attachment 1009043 [details]
production.log

Comment 20 Lukas Zapletal 2015-04-01 08:24:31 UTC
Hmmm works for me. Can you give us exact steps how to reproduce this on a cleanly installed Sat 6 ? Please note this must not be an upgrade from previous snap, you must install the snap from scratch.

Comment 21 Sachin Ghai 2015-04-01 08:31:05 UTC
Its reproducible on clean/Fresh installed Sat6 box with snap8.

Steps to reproduce:
====================
1. discover a host
2. Provision the discover host manually..But while updating the new host form, fill something with wrong value.. may be don't specify the operating system and submit the form
3. delete the host

Comment 24 Sachin Ghai 2015-05-04 12:23:37 UTC
Issue is re-producible with Sat6.1 GA snap2(Satellite-6.1.0-RHEL-7-20150429.2). On deleting a discovered host, UI raises error:

-
ERROR: update or delete on table "hosts" violates foreign key constraint "tokens_host_id_fk" on table "tokens" DETAIL: Key (id)=(2) is still referenced from table "tokens". 
-

Comment 25 Sachin Ghai 2015-05-04 12:24:15 UTC
Created attachment 1021734 [details]
reproducible with GA snap2

Comment 26 orabin 2015-05-04 12:31:30 UTC
Is this a clean install or an update?
The migration was merged before the full fix so if this is an update it needs to be rerun before testing.

Comment 27 Sachin Ghai 2015-05-04 12:46:13 UTC
Its a clean install..without upgrade..

Comment 28 orabin 2015-05-05 10:53:59 UTC
Opened up a related bug upstream: http://projects.theforeman.org/issues/10365
moving this to POST as the fix has been merged upstream

Comment 30 Lukas Zapletal 2015-05-06 06:57:44 UTC
Scheduled for re-triage, this is a blocker. We have an upstream patch already.

Comment 31 Sachin Ghai 2015-05-15 09:11:48 UTC
This issue is also reproducible with snap4 compose2.

2015-05-15 13:32:29 [I] Processing by HostsController#destroy as HTML
2015-05-15 13:32:29 [I]   Parameters: {"authenticity_token"=>"rSnD0zt242mRrI5cJhkRpZyfzHfRNJhkSAOtaKodQlY=", "id"=>"mac525400d03216.lab.eng.pnq.redhat.com"}
2015-05-15 13:32:30 [E] PGError: ERROR:  update or delete on table "hosts" violates foreign key constraint "tokens_host_id_fk" on table "tokens"
DETAIL:  Key (id)=(11) is still referenced from table "tokens".
: DELETE FROM "hosts" WHERE "hosts"."type" IN ('Host::Managed') AND "hosts"."id" = $1 (ActiveRecord::InvalidForeignKey)
/opt/rh/ruby193/root/usr/share/gems/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:1171:in `get_last_result'
/opt/rh/ruby193/root/usr/share/gems/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:1171:in `exec_cache'
/opt/rh/ruby193/root/usr/share/gems/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:676:in `block in exec_delete'

Comment 36 Sachin Ghai 2015-05-26 10:49:41 UTC
Moving this to verified as I tried all possible ways to reproduce the issue but looks like its fixed by all ways. Thank you for fixing it

Comment 37 Lukas Zapletal 2015-06-11 14:55:53 UTC
WORKAROUND:

# foreman-rake console
> Token.where('id not in (?)', Host::Managed.all.map(&:token).compact).delete_all
> exit

Then delete the discovered host. Until this fix is delivered in 6.1 GA, this must be repeated every time you want to delete an invalid host.

Comment 38 Bryan Kearney 2015-08-11 13:36:40 UTC
This bug is slated to be released with Satellite 6.1.

Comment 39 errata-xmlrpc 2015-08-12 05:28:14 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory, and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHSA-2015:1592


Note You need to log in before you can comment on or make changes to this bug.