If I understand correctly, you have a row with its parent_id
column containing 2
(for example), but there is no row with ID 2
in the table.
If so, then it's normal to get an exception. Clean your data by setting all those inexistent parent_id
to NULL, and add a foreign key constraint to the parent_id
column so that this situation never happens anymore.