hi guys,

i'm wondering how transaction really works with django models. i've
gone through the documentation in 
http://docs.djangoproject.com/en/dev/topics/db/transactions/?from=olddocs
but it doesn't work for me.

this is a sample of my code. not that this code is being used in the
backend and does not have any relationship to any views.

model relationships
-------------------------------

class A(models.Model):
   login = models.EmailField(unique=True)

class B(models.Model):
   aid = models.OneToOneField(A, primary_key=True)
   colb = models.CharField(max_length=255)

class C(models.Model):
   aid = models.OneToOneField(A, primary_key=True)
   colc = models.CharField(max_length=255)

code
----------------

class AClass(object):
  def register(cls, **kwargs):
      try:
          a_db = A(login=kwargs['login'])
          a_db.save()
          function_test_01()
          return a_db
      except:
          raise MyException

class BClass(AClass):
  def register(cls, **kwargs):
      try:
          a_db =  super(BClass, cls).register(**kwargs)
          b_db = B(aid=a_db, colb=kwargs['colb'])
          b_db.save()
          function_test_02()
          return a_db
      exception:
          raise MyException

class CClass(BClass):

   @transaction.commit_manually
   def register(cls, **kwargs):
      try:
          a_db = super(CClass, cls).register(**kwargs)
          c_db = C(aid=a_db, colc=kwargs['colc'])
          c_db.save()
          transaction.commit()
      except:
          transaction.rollback()

if __name__ == '__main__':
    # Test if register works
    CClass.register( colc='abc', colb='def')


If I run the above script no errors prop up, all three tables will be
populated with the data and everything is ok. On the other hand, if
something goes wrong in the middle somehwhere, my transaction doesn't
work the way i wanted it to.

For example, when running the test, function_test_02() raises an
exception and this is caught by CClass.register()'s except and
transaction.rollback() is supposed to be called, but the data in
AClass register() was entered into the db. As a result, I have a
situation where the only data inserted into the db is A while B and C
was not.

Can anyone point out where I'm doing it wrong?

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to