Bug #26733

Foreign-key violation on upgrade - AddContentConstraints

Added by Partha Aji about 3 years ago. Updated about 3 years ago.

Target version:
Bugzilla link:
Fixed in Releases:
Found in Releases:
Red Hat JIRA:


Few users have hit the following error while upgrading

** Execute db:migrate
== 20190205142619 AddContentConstraints: migrating ============================
-- change_column(:katello_product_contents, :content_id, :integer, {:null=>false})
   -> 0.0038s
-- change_column(:katello_product_contents, :product_id, :integer, {:null=>false})
   -> 0.0018s
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::ForeignKeyViolation: ERROR:  update or delete on table "katello_contents" violates foreign key constraint "katello_product_content_content_id_fk" on table "katello_product_contents" 
DETAIL:  Key (id)=(2403) is still referenced from table "katello_product_contents".
: DELETE FROM "katello_contents" WHERE "katello_contents"."id" = $1

It looks like users had dirty data before upgrading so we had to make the migration more robust. To simulate the bad data I had to do the following from the latest on rails console

$ cd <katello git dir>
$ git checkout master
$ cd <foreman git>
$ rails c
#  remove unique constraints since we are going to create duplicated data.
>ActiveRecord::Migration.remove_index :katello_contents, :name => :katello_contents_cpcid_orgid_uniq
> pc = Katello::ProdiuctContent.last 
# create duplicated data
> content = pc.content.dup
> dup_pc = pc.dup
> dup_pc.content = content
# Now run the migration
> require '../katello/db/migrate/20190205142619_add_content_constraints.rb'

So this will create duplicated ProductContent and Content rows. We then run the following migration -> the code should fail and report the above error.

Now check out this PR and rerun

$ rails c
> require '../katello/db/migrate/20190205142619_add_content_constraints.rb'

That should be successful.

Associated revisions

Revision 6f45d27e (diff)
Added by Partha Aji about 3 years ago

Fixes #26733 - Delete on bad content_constraints (#8096)

Updated add_content_constraints migration to properly delete the
associated product_content before deleting the content itself. The prior
migration code did not handle the case where 2 duplicated rows of
content pointed to the same product.


#1 Updated by Partha Aji about 3 years ago

  • Assignee set to Partha Aji

#2 Updated by Partha Aji about 3 years ago

  • Bugzilla link set to 1669186

#3 Updated by The Foreman Bot about 3 years ago

  • Status changed from New to Ready For Testing
  • Pull request added

#4 Updated by Anonymous about 3 years ago

  • Status changed from Ready For Testing to Closed

#5 Updated by Jonathon Turel about 3 years ago

  • Triaged changed from No to Yes
  • Target version set to Katello 3.10.2

#6 Updated by Jonathon Turel about 3 years ago

  • Target version changed from Katello 3.10.2 to Katello 3.12.1

Also available in: Atom PDF