0

While debugging a unit test in Django, I placed the following statement in my code

Student.objects.filter(id__in=[1,2]).delete()
...
import pdb; pdb.set_trace()

At the breakpoint, if I type Student.objects.count(), I can see that it has reduced due to delete however if I open psql (PostgreSQL command line) and check the test_database, I can still see the rows (which have been deleted according to Django). Why do I see this inconsistency between Django ORM & the database. Does the ORM cache my queries? How can I make it commit to the database at the breakpoint?

Update:

Quick solution for debugging. Add the following lines at breakpoint to see the data in psql. Thanks to @DanielRoseman for the tip.

    from django.db import connection
    connection.cursor().execute('commit;')
user
  • 17,781
  • 20
  • 98
  • 124

1 Answers1

2

Django requests usually run inside a transaction. Your psql session won't see the changes until the transaction is committed when the session returns.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Thanks for pointing that out. Makes sense after reading up a bit on transactions. Otherwise, it was baffling for someone not familiar with the inner workings. – user Dec 28 '14 at 19:09