2

I have a Django app that I have just migrated to South for model/DB synchronization. I have created a new migration after making some changes to the model (ie. adding a new foreign key field). Here is a sample of the code within the new migration 0002_auto__add_field_table_new_field.py

class Migration(SchemaMigration):

    def forwards(self, orm):
        # Adding field 'Table.new_field'
        db.add_column(u'Table', 'new_field',
                      self.gf('django.db.models.fields.related.ForeignKey')(to=orm['database.other_table'], null=True, blank=True),
                      keep_default=False)


    def backwards(self, orm):
        # Deleting field 'Table.new_field'
        db.delete_column(u'Table', 'new_field')

When I attempt to apply the migration the query always times out after roughly 30 seconds, with the following error message:

sqlserver_ado,dbapi.DatabaseError: (-2147352567, 'Exception occured.', (0, u'Microsoft SQL Server Native Client 10.0', u'Query timeout expired', None, 0, -2147217871), None)

Is it possible to increase the SQL Server query timeout? I have been unable to find any specific documentation on this other than how to increase the value within SSMS, but having done this it has made no difference. Is this perhaps done in Django settings.py?

giogoice
  • 41
  • 1
  • 8

3 Answers3

2

This question is a bit old, but it's what comes up at the top of Google's results, so I'd like to add for those coming along later that if you use pyodbc with django-pyodbc (specifically django-pyodbc-azure for more modern Django), there are several settings that may be of interest; note especially the query_timeout setting:

  • connection_timeout

    Integer. Sets the timeout in seconds for the database connection process. Default value is 0 which disables the timeout.

  • connection_retries

    Integer. Sets the times to retry the database connection process. Default value is 5.

  • connection_retry_backoff_time

    Integer. Sets the back off time in seconds for reries of the database connection process. Default value is 5.

  • query_timeout

    Integer. Sets the timeout in seconds for the database query. Default value is 0 which disables the timeout.

  • Adding the query_timeout setting to the database options allowed me to work around an issue that was occurring to a db lock (where the write my code was doing wasn't currently important):

    DATABASES = {
        'default': {
            # ...
            'OPTIONS': {
                'driver': 'FreeTDS',
                'query_timeout': 6,
            },
        },
    }
    
    hlongmore
    • 1,603
    • 24
    • 28
    0

    So it turns out that the problem was being caused by the backend python db module sqlserver_ado. It is this module that was determining the query timeout. I have amended my settings.py to now use the sql_server.pyodbc ENGINE instead. The migration has now been applied successfully by making use of this.

    giogoice
    • 41
    • 1
    • 8
    • Hi, I'm having a similar problem in that I want to change the timeout. Were you using django-mssql? How did you use pyodbc with django? – user193130 Dec 10 '14 at 23:34
    0

    If you need to stick with the sqlserver_ado driver, you can set query timeout as described in this question (default is 30 s): Query timeout expired in django-mssql when executing custom SQL directly

    DATABASES = {
    'default': {
        'NAME': DATABASE_NAME,
        'ENGINE': 'sqlserver_ado',
        'HOST': DATABASE_HOST,
        'USER': DATABASE_USER,
        'PASSWORD': DATABASE_PASSWORD,
        'COMMAND_TIMEOUT': timeout_in_seconds,
       }
    }
    
    Community
    • 1
    • 1
    Mr. Napik
    • 5,499
    • 3
    • 24
    • 18