I reproduced ordinal issue with foreign key constraint violation. I worked though this thread and I found it doesn't help me. What helped me is this one, reseeding the index
DBCC CHECKIDENT ('schema.customer', RESEED, 0);
After index was reseed, exactly the same junit test started to work.
I noticed that issue starts to reproduce after an exception thrown. In my case I'm developing a test, making some mistake that causes test to stop and unwind a thread after parent record was already inserted and child not. I fix the test, but it doesn't work because of constraint violation. Deletion of all data in parent and child tables do not help.
Junit test is spring integration test that creates test spring context with connection to MS SQL Server, mybatis mappers are autowired and used in the test. No explicit transaction management. Dummy record insertion and selection and data integrity validation.
Could you please explain me what happens here? I scare to reproduce the same in prod. I would not reseed index and delete all data there. The issue is intermittent, I came to the same 'infinite fail loop' several times. After index manipulation it start to work again.
When running several tests with insertions / removals issue started to reproduce 100% of times. I end-up with FK removal for all tables which fixed the issue permanently