Skip to content

ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: insert or update on table "merge_request_metrics" violates forei...

https://sentry.gitlab.net/gitlab/gitlabcom/issues/1213898/

PG::ForeignKeyViolation: ERROR:  insert or update on table "merge_request_metrics" violates foreign key constraint "fk_rails_e6d7c24d1b"
DETAIL:  Key (merge_request_id)=(48285446) is not present in table "merge_requests".

  active_record/connection_adapters/postgresql_adapter.rb:672:in `exec_params'
    @connection.exec_params(sql, type_casted_binds)
  active_record/connection_adapters/postgresql_adapter.rb:672:in `block (2 levels) in exec_no_cache'
    @connection.exec_params(sql, type_casted_binds)
  active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads'
    yield
  active_support/concurrency/share_lock.rb:187:in `yield_shares'
    yield
  active_support/dependencies/interlock.rb:47:in `permit_concurrent_loads'
    @lock.yield_shares(compatible: [:load]) do
...
(187 additional frame(s) were not displayed)

ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR:  insert or update on table "merge_request_metrics" violates foreign key constraint "fk_rails_e6d7c24d1b"
DETAIL:  Key (merge_request_id)=(48285446) is not present in table "merge_requests".

PG::ForeignKeyViolation: ERROR:  insert or update on table "merge_request_metrics" violates foreign key constraint "fk_rails_e6d7c24d1b"
DETAIL:  Key (merge_request_id)=(48285446) is not present in table "merge_requests".

Possible solutions

It seems the Merge Request is being deleted before the Merge Request Metrics record has a chance to be created. So it seems the Merge Request Metrics INSERT query should be allowed to fail gracefully.

Edited by Dan Jensen