This happens because the enzyme nitrogenase, that is responsible for the reduction of N2, is extremely sensitive to O2. So, cyanobacteria evolved mechanisms to separate the processes of photosynthesis and N2 fixation. The separation could be:
• Spatial- It refers to heterocysts-cells with thick walls that keep the oxygen concentration low. Those cells also lack the oxygenic photosystem, so the nitrogen fixation can occur.
• Temporal- fixing nitrogen during the night.