question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Foreign key constraint violation during `clearDatabase`

See original GitHub issue

Short problem description

Unable to clear database completely because of a foreign key constraints violation.

Software versions

  • postgres: 11.5
  • database-rider: rider-spring:1.10.0 with underlying rider-core:1.10.0

Steps to reproduce

⚠️ To reproduce this problem you should have Docker installed on your environment.

  1. Clone the repo https://github.com/iakunin/codexia-bot:
git clone git@github.com:iakunin/codexia-bot.git && cd codexia-bot
  1. Checkout to specific branch:
git checkout rider-fk-violation
  1. Run the test with problem
bash bin/gradle_in_docker.sh clean test --info --tests *SendReviewsIntegrationTest.reviewSentWith500
  1. Grep the postgres container logs
cat var/pg-container.log |  grep -v "SELECT NULL AS TABLE_CAT" | grep -v "FROM pg_catalog.pg_type" | grep -v "pg_catalog.pg_namespace" | tail -n16

And you will see the following output:

2020-04-17 11:26:45.671 UTC [69] LOG:  execute <unnamed>: DELETE FROM public.codexia_project where 1=1
2020-04-17 11:26:45.671 UTC [69] ERROR:  update or delete on table "codexia_project" violates foreign key constraint "codexia_review_codexia_project_id_fkey" on table "codexia_review"
2020-04-17 11:26:45.671 UTC [69] DETAIL:  Key (id)=(5) is still referenced from table "codexia_review".
2020-04-17 11:26:45.671 UTC [69] STATEMENT:  DELETE FROM public.codexia_project where 1=1
2020-04-17 11:26:45.672 UTC [69] LOG:  execute <unnamed>: DELETE FROM public.codexia_review where 1=1
2020-04-17 11:26:45.672 UTC [69] ERROR:  update or delete on table "codexia_review" violates foreign key constraint "codexia_review_notification_codexia_review_id_fkey" on table "codexia_review_notification"
2020-04-17 11:26:45.672 UTC [69] DETAIL:  Key (id)=(1) is still referenced from table "codexia_review_notification".
2020-04-17 11:26:45.672 UTC [69] STATEMENT:  DELETE FROM public.codexia_review where 1=1
2020-04-17 11:26:45.673 UTC [69] LOG:  execute <unnamed>: DELETE FROM public.codexia_review_notification where 1=1
2020-04-17 11:26:45.675 UTC [69] LOG:  execute <unnamed>: DELETE FROM public.databasechangelog where 1=1
2020-04-17 11:26:45.676 UTC [69] LOG:  execute <unnamed>: DELETE FROM public.databasechangeloglock where 1=1
2020-04-17 11:26:45.676 UTC [69] LOG:  execute <unnamed>: DELETE FROM public.github_repo where 1=1
2020-04-17 11:26:45.677 UTC [69] LOG:  execute <unnamed>: DELETE FROM public.github_repo_source where 1=1
2020-04-17 11:26:45.678 UTC [69] LOG:  execute <unnamed>: DELETE FROM public.github_repo_stat where 1=1
2020-04-17 11:26:45.679 UTC [69] LOG:  execute <unnamed>: DELETE FROM public.hackernews_item where 1=1
2020-04-17 11:26:45.680 UTC [69] LOG:  execute <unnamed>: DELETE FROM public.stars_up_result where 1=1

Pay attention to this: ERROR: update or delete on table "codexia_project" violates foreign key constraint "codexia_review_codexia_project_id_fkey" on table "codexia_review". I think, that is pretty clear sign of a problem.

As a result, the table codexia_project is not cleared as expected.

It is worth mentioning that corresponding exception is not rethrown, but just logged and swallowed (link to source).

Suggested solution

Decorate the com.github.database.rider.core.dataset.DataSetExecutorImpl#clearDatabase method with disableConstraints() call before and with enableConstraints() call after.

Something like this:

/**
 * @throws SQLException if clean up cannot be performed
 */
@Override
public void clearDatabase(DataSetConfig config) throws SQLException {

    disableConstraints();

    // the original code left intact

    enableConstraints();
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:29 (28 by maintainers)

github_iconTop GitHub Comments

1reaction
arhohuttunencommented, May 8, 2020

I wasn’t aware that the JUnit 5 module is not hooking up to the Spring transaction context. We are pretty much using @DataJpaTest everywhere instead of @SpringBootTest. This definitely happens because of the transactions. I’ll continue the discussion in the other issue.

1reaction
rmpestanocommented, Apr 17, 2020

This issue was fixed by #203 and will be available in v1.13.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

3 Common Foreign Key Mistakes (And How to Avoid Them)
Foreign key constraints are important to any SQL database, but they can also cause problems if they're not implemented correctly.
Read more >
Foreign key constraint violation when loading or deleting data ...
Foreign key constraint violation when loading or deleting data in database. When the destination is a database, and there are multiple source- ...
Read more >
foreign key constraint violation issue - DBA Stack Exchange
When a student is created with enrollments, but no grades have been assigned, that student has no entry in the GPA table. When...
Read more >
Mysql: Integrity constraint violation - Stack Overflow
I am using cleardb for mysql database in php . ... violation: 1452 Cannot add or update a child row: a foreign key...
Read more >
Troubleshoot an AWS DMS Task That Failed With a Foreign ...
... DMS) task that is failing with a foreign key constraint violation. ... By default, AWS DMS tasks load eight tables at a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found