<p>The bug allows a newly-created element to refer to a deleted one if the transactions for both overlap. Precisely, the issue is that the check that an element exists does not prevent a concurrent transaction from altering that row.</p>

<p>Because "deleting" an element in the OSM database does not remove the row, we cannot rely on FK constraints to ensure the correct behaviour. Instead, this fix relies on manually locking referenced elements.</p>

<p>Note that this "fix" is suboptimal, as it does not allow any updates to the referenced elements. Updates which do not delete the row could safely be done, but will be prevented.</p>

<p>Also, it's not clear what the negative performance impact of this change will be.</p>

<hr>

<h4>You can view, comment on, or merge this pull request online at:</h4>
<p>  <a href='https://github.com/openstreetmap/openstreetmap-website/pull/992'>https://github.com/openstreetmap/openstreetmap-website/pull/992</a></p>

<h4>Commit Summary</h4>
<ul>
  <li>Fix bug allowing created elements to reference deleted ones.</li>
</ul>

<h4>File Changes</h4>
<ul>
  <li>
    <strong>M</strong>
    <a href="https://github.com/openstreetmap/openstreetmap-website/pull/992/files#diff-0">app/models/relation.rb</a>
    (3)
  </li>
  <li>
    <strong>M</strong>
    <a href="https://github.com/openstreetmap/openstreetmap-website/pull/992/files#diff-1">app/models/way.rb</a>
    (4)
  </li>
</ul>

<h4>Patch Links:</h4>
<ul>
  <li><a href='https://github.com/openstreetmap/openstreetmap-website/pull/992.patch'>https://github.com/openstreetmap/openstreetmap-website/pull/992.patch</a></li>
  <li><a href='https://github.com/openstreetmap/openstreetmap-website/pull/992.diff'>https://github.com/openstreetmap/openstreetmap-website/pull/992.diff</a></li>
</ul>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br>Reply to this email directly or <a href="https://github.com/openstreetmap/openstreetmap-website/pull/992">view it on GitHub</a>.<img alt="" height="1" src="https://github.com/notifications/beacon/ABWnLTyHI3NOEk-KFdbCT9clQEOQ5F0cks5oS2wwgaJpZM4FBkjZ.gif" width="1" /></p>
<div itemscope itemtype="http://schema.org/EmailMessage">
  <div itemprop="action" itemscope itemtype="http://schema.org/ViewAction">
    <link itemprop="url" href="https://github.com/openstreetmap/openstreetmap-website/pull/992"></link>
    <meta itemprop="name" content="View Pull Request"></meta>
  </div>
  <meta itemprop="description" content="View this Pull Request on GitHub"></meta>
</div>