title: Django foreign key query returns records that are not in the database
body: <p>I have a strange situation where Django seems to be giving me records that do not actually exist in the database when queried via a related_name on a foreign key.  Here's a simplified example:</p>
  <p>Let's say I have a Person model and a Pet model, where each Pet has an owner, which is a foreign key on Person:</p>
  <pre><code>class Pet(models.Model):
      id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
      name =models.CharField(max_length=50, null=False, db_index=True)
      owner = models.ForeignKey(&quot;Person&quot;, null=False, related_name=&quot;pets&quot;, on_delete=models.CASCADE)
      relationship = models.IntegerField(null=False, choices=PetRelationship.choices(), db_index=True)
  </code></pre>
  <p>Now, I have the below function that retrieves a person's pets:</p>
  <pre><code>def pet_tester(person):
      for pet in person.pets.filter(relationship=PetRelationship.FRIENDLY):
          pet_id = pet.id
          LOGGER.info(f&quot;*************** pet.id = {pet_id}&quot;)
          LOGGER.info(f&quot;******* Pet exists? = {Pet.objects.filter(id=pet_id).exists()}&quot;)
          ...
  </code></pre>
  <p>Note that this is NOT a &quot;minimal reproducible example&quot;.  I can only reproduce it in my much larger application.</p>
  <p>For some reason (in the real app), the &quot;exists&quot; query is coming back as False.  The output is like this:</p>
  <pre><code>*************** pet.id = 123e4567-e89b-12d3-a456-426614174000
  ******* Pet exists? = False
  </code></pre>
  <p>If I query the actual database (Postgresql) directly (outside of Django), that pet ID sure enough does NOT exist.  However, the <code>person.pets.filter</code> query is returning it just the same.</p>
  <p>I do not understand how this is even possible. It JUST retrieved the pet from the database (or so it would seem - it even has a UUID), but it's not really there after all when I immediately try to query it back.</p>
  <p>It seems like the Django &quot;filter&quot; results are somehow inconsistent with the underlying database, but I don't know why. This is very reproducible in my app (it happens every time, consistently).  It doesn't appear to be a corrupt database, as it happens consistently even on brand new machines, with the app and database freshly installed.</p>
  <p>I know you don't know what else my app is doing. I obviously can't post all the source code, but I can assure you that the beginning of the actual function is pretty much verbatim of the pet_tester function above. I only changed the class/field names.  There are no extra lines in the actual app between the &quot;for&quot; line and the 3 lines below it.</p>
  <p>I'm mostly looking for ideas about how this can possibly happen, and what to look for in the app's other code.</p>