Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autocommit Migration from Django 1.7 to 1.8

Tags:

python

django

I was migrating from Django 1.7 to 1.8 via following steps

  1. Active virtualenv
  2. Uninstall Django 1.7
  3. Install Django 1.8
  4. python manage.py runserver

On execution of step 4 for I am getting the following error.

Unhandled exception in thread started by <function wrapper at 0x7f4e473a8230>
Traceback (most recent call last):
  File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 223, in wrapper
    fn(*args, **kwargs)
  File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 112, in inner_run
    self.check_migrations()
  File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 164, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 19, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 47, in __init__
    self.build_graph()
  File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 180, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 59, in applied_migrations
    self.ensure_schema()
  File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 49, in ensure_schema
    if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
  File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 162, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 135, in _cursor
    self.ensure_connection()
  File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 130, in ensure_connection
    self.connect()
  File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/django/db/utils.py", line 97, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 130, in ensure_connection
    self.connect()
  File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 119, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 172, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/psycopg2/__init__.py", line 164, in connect
    conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: invalid connection option "autocommit"

Error seems to be from psycopg2 module and related to autocommit. In the feature removed section of 1.8 documentation I found the following line.

the decorators and context managers autocommit, commit_on_success, and commit_manually, defined in django.db.transaction

I couldn't relate this to the error I got. Can somebody throw light into this?

Update:

I found out why. Below is my DB connection config. In that there is autocommit=True. On commenting that line, the issue got resolved. But still I want to know why we cannot give autocommit=True option.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'HOST': 'localhost',
        'PORT': '5432',
        'NAME': 'bp_django_auth',
        'USER': 'postgres',
        'PASSWORD': 'abcd1234',
        'OPTIONS': {
            "autocommit": True,
        },
    }
}
like image 673
Arun Ghosh Avatar asked Apr 04 '15 05:04

Arun Ghosh


People also ask

How does Django know which migrations to run?

Ensuring that a migration is applied only once requires keeping track of the migrations that have been applied. Django uses a database table called django_migrations . Django automatically creates this table in your database the first time you apply any migrations.

Which command is used for migration in Django?

The Commands There are several commands which you will use to interact with migrations and Django's handling of database schema: migrate , which is responsible for applying and unapplying migrations. makemigrations , which is responsible for creating new migrations based on the changes you have made to your models.

Where are Django migration stored?

Migrations are generated per app, and are stored in some_app/migrations . Even if you do not define migrations for your apps, there will usually be migrations that take place, since you (likely) included some apps defined by Django (and other third parties) in your INSTALLED_APPS , these have migrations as well.


1 Answers

The following was outlined in the Django 1.7 Databases docs:

In previous versions of Django, database-level autocommit could be enabled by setting the autocommit key in the OPTIONS part of your database configuration in DATABASES.

Since Django 1.6, autocommit is turned on by default. This configuration is ignored and can be safely removed.

And as per the 1.8 Release Notes, this feature was removed.

If you still want to keep the setting for some reason, simply move it out of OPTIONS:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'HOST': 'localhost',
        'PORT': '5432',
        'NAME': 'bp_django_auth',
        'USER': 'postgres',
        'PASSWORD': 'abcd1234',
        'AUTOCOMMIT': True,
    }
}
like image 148
rnevius Avatar answered Oct 31 '22 19:10

rnevius