﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
12118	in-memory test database does not work with threads	bluebird75	Andriy Sokolovskiy	"When using the test configuration of the DB with XXX and accessing the DB from another thread, it fails miserably.

Using this example script on the Poll tutorial:

{{{
import os                                                                       
os.environ[ 'DJANGO_SETTINGS_MODULE' ] = 'settings'                             
import settings                                                                 
import datetime, threading                                                      
                                                                                
#django  stuff                                                                  
from polls.models import *                                                      
from django.core.mail import mail_admins                                        
from django.test.utils import *                                                 
from django.db import connection                                                
                                                                                
                                                                                
def create_object():                                                            
    print 'Creating Poll'                                                       
    p = Poll()                                                                  
    p.question = ""What's up doc ?""                                              
    p.pub_date = datetime.date.today()                                          
    p.save()                                                                    
    print 'Poll object saved. Id: %d' % p.id                                    
                                                                                
                                                                                
WITH_THREAD = False                                                             
                                                                                
if __name__ == '__main__':                                                      
    setup_test_environment()                                                    
    old_db_name = settings.DATABASE_NAME                                        
    new_db_name = connection.creation.create_test_db(verbosity=1)               
    print 'New DATABASE:', new_db_name                                          
                                                                                
    if WITH_THREAD:                                                             
        t = threading.Thread( target=create_object )                            
        t.start()                                                               
        t.join()                                                                
    else:                                                                       
        create_object()                                                         
                                                                                
    teardown_test_environment()                                                 
    connection.creation.destroy_test_db( old_db_name )             

}}}

If I run it with WITH_THREADS set to False:
 

{{{
Philippe@pc-philippe /cygdrive/d/work/django/django-tuto/mysite $ python run_wi 
th_threads.py                                                                   
Creating test database...                                                       
Creating table auth_permission                                                  
Creating table auth_group                                                       
Creating table auth_user                                                        
Creating table auth_message                                                     
Creating table django_admin_log                                                 
Creating table django_content_type                                              
Creating table django_session                                                   
Creating table django_site                                                      
Creating table polls_poll                                                       
Creating table polls_choice                                                     
Installing index for auth.Permission model                                      
Installing index for auth.Message model                                         
Installing index for admin.LogEntry model                                       
Installing index for polls.Choice model                                         
New DATABASE: :memory:                                                          
Creating Poll                                                                   
Poll object saved. Id: 1                                                        
Destroying test database...   

}}}

If I run it with WITH_THREADS set to True:


{{{
Philippe@pc-philippe /cygdrive/d/work/django/django-tuto/mysite $ python run_wi 
th_threads.py                                                                   
Creating test database...                                                       
Creating table auth_permission                                                  
Creating table auth_group                                                       
Creating table auth_user                                                        
Creating table auth_message                                                     
Creating table django_admin_log                                                 
Creating table django_content_type                                              
Creating table django_session                                                   
Creating table django_site                                                      
Creating table polls_poll                                                       
Creating table polls_choice                                                     
Installing index for auth.Permission model                                      
Installing index for auth.Message model                                         
Installing index for admin.LogEntry model                                       
Installing index for polls.Choice model                                         
New DATABASE: :memory:                                                          
Creating Poll                                                                   
Exception in thread Thread-1:                                                   
Traceback (most recent call last):                                              
  File ""c:\Python26\lib\threading.py"", line 522, in __bootstrap_inner           
    self.run()                                                                  
  File ""c:\Python26\lib\threading.py"", line 477, in run                         
    self.__target(*self.__args, **self.__kwargs)                                
  File ""run_with_threads.py"", line 19, in create_object                         
    p.save()                                                                    
  File ""c:\Python26\lib\site-packages\django\db\models\base.py"", line 410, in save                                                                              
    self.save_base(force_insert=force_insert, force_update=force_update)        
  File ""c:\Python26\lib\site-packages\django\db\models\base.py"", line 495, in save_base                                                                         
    result = manager._insert(values, return_id=update_pk)                       
  File ""c:\Python26\lib\site-packages\django\db\models\manager.py"", line 177, in _insert                                                                        
    return insert_query(self.model, values, **kwargs)                           
  File ""c:\Python26\lib\site-packages\django\db\models\query.py"", line 1087, in 
insert_query                                                                    
    return query.execute_sql(return_id)                                         
  File ""c:\Python26\lib\site-packages\django\db\models\sql\subqueries.py"", line 
320, in execute_sql                                                             
    cursor = super(InsertQuery, self).execute_sql(None)                         
  File ""c:\Python26\lib\site-packages\django\db\models\sql\query.py"", line 2369, in execute_sql                                                                 
    cursor.execute(sql, params)                                                 
  File ""c:\Python26\lib\site-packages\django\db\backends\util.py"", line 19, in execute                                                                          
    return self.cursor.execute(sql, params)                                     
  File ""c:\Python26\lib\site-packages\django\db\backends\sqlite3\base.py"", line 
193, in execute                                                                 
    return Database.Cursor.execute(self, query, params)                         
OperationalError: no such table: polls_poll                                     
                                                                                
Destroying test database...  

}}}


"	New feature	closed	Database layer (models, ORM)	dev	Normal	fixed	threads		Accepted	1	0	0	1	0	0
