#15243: commit_unless_managed clarification for multiple databases in the docs
-----------------------------------------------------------------------------+
 Reporter:  toqueteos@…                                                      |  
        Owner:  nobody    
   Status:  new                                                              |  
    Milestone:            
Component:  Documentation                                                    |  
      Version:  1.2       
 Keywords:  transaction, commit_unless_managed, raw sql, multiple databases  |  
 Triage Stage:  Unreviewed
Has patch:  0                                                                |  
-----------------------------------------------------------------------------+
 First ticket of mine here guys.

 ( Please be nice )

 Today I was working with a two databases setup: '''default''' and
 '''my_other_db'''.

 I wrote a function to execute some queries on '''my_other_db'''.

 Example code:
 {{{#!python
 def email_update(email, password):
     """
     Update a email account from the database with a new password.
     """
     cursor = connections["my_other_db"].cursor()
     query = "UPDATE users SET password=ENCRYPT(%s) WHERE email=%s"
     # Perform the query
     cursor.execute(query, [password, email])
     # Hey Django, ensure changes are done to the DB
     transaction.commit_unless_managed()
 }}}

 So the problem is that the last line does '''nothing'''.

 Well, it does something, a rollback on the query because I'm not hitting
 the right db, the one I chose from the '''connections''' dict.

 Specifying, again, the database alias name on the commit_unless_managed
 method, with the '''using''' keyword argument makes the function to work.

 Example:
 {{{#!python
 transaction.commit_unless_managed(using="my_other_db")
 }}}

 I think, a tiny one-two lines note placed below the third paragraph of the
 section called '''Executing custom SQL directly''' at
 http://docs.djangoproject.com/en/dev/topics/db/sql/#executing-custom-sql-
 directly should be ok.

 Hope it helps.

 ----

 Related:

 Where's the DRY principle of Django here?

 That transaction should look for the database alias used with the cursor,
 right?

-- 
Ticket URL: <http://code.djangoproject.com/ticket/15243>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to