Greetings,
I've been wrestling with a very strange set of errors in Django 1.1.1
using psycopg2 as the back end, and DATABASE_OPTIONS "autocommit":
True feature.
I was looking for the right combination of options and decorators such
that Django would not automatically wrap view functions in BEGIN/
COMMIT blocks in the database, unless such were coded in. However,
this rapidly went down a strange path:
In my application (attached below; it's simple), the *first time* I
access /createorder, it fails with a ProgrammingError. Looking at the
log from the database, the programming error is caused by Django
issuing an INSERT without a RETURNING clause, where it expects the
RETURNING clause to be there (to get the id of the newly-created
object). The *second* time I run it, it works correctly. This is
true even if I precompile the application, so it's not a compilation
issue.
Further, when accessing /showorders/(order number), it sometimes
issues the BEGIN/ROLLBACK to the database server, and sometimes does
not, and I cannot for the life of me understand why it varies.
This is running these through the development server.
Any thoughts? Anything obviously broken?
--
The application is "example", in project "djangotrans":
---- settings.py
No transaction middleware, and added:
DATABASE_OPTIONS = {
"autocommit": True,
}
---- urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns('djangotrans.example.views',
(r'^createorder/$', 'create_order'),
(r'^showorder/(?P<order_id>\d+)$', 'show_order'),
)
---- example/views.py:
from django.http import HttpResponse
from djangotrans.example.models import Order, Address
from django.db import transaction
@transaction.commit_on_success
def create_order(request):
address = Address(street_address="1112 E Broad St",
city="Westfield NJ", state="CA", zip="07090")
address.save()
order = Order(customer_name="Gomez Addams",
shipping_address=address)
order.save()
return HttpResponse("Created: " + unicode(order))
def show_order(request, order_id):
order = Order.objects.get(pk=order_id)
return HttpResponse(unicode(order))
---- example/models.py
from django.db import models
class Address(models.Model):
street_address = models.CharField(max_length=80)
city = models.CharField(max_length=80)
state = models.CharField(max_length=2)
zip = models.CharField(max_length=5)
def __unicode__(self):
return self.street_address + " " + self.city + " " +
self.state + " " + self.zip
class Order(models.Model):
customer_name = models.CharField(max_length=80)
shipping_address = models.ForeignKey(Address)
def __unicode__(self):
return "Order " + unicode(self.id) + " going to " + \
self.customer_name + ", " + unicode(self.shipping_address)
--
-- Christophe Pettus
[email protected]
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---