Importing Encounters from a Data Recovery

I was able to create a script that imported encounters that were lost during three days of data loss. The problem I am experiencing is that new encounters are being created with the same encounter numbers that I brought in. I looked through the code and it seems that OpenEMR doesn’t check for an existing encounter number, it just created one with the number it thinks it should. Is there a way to prevent OpenEMR from creating encounters that have the same encounter number as the ones I imported?

I have tracked the code down to the following line of code in encounter_events.inc

I could really use some advice here. How would I import these encounters and not compromise new data coming in?

think it uses the sequences table

That seems correct. Thank you

I’m having a tough time understanding the reasoning for how this behaves. I should have never been able to write to the encounter table with a value larger than the sequence table, especially if the write was going to cause instability.