Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyTest-Django Failing on missing django_migration table

I'm trying to add pytest-django to my current pytest3/Django1.7 environment.

Currently we have not been using the plugin and have been suffering from shared state between certain tests

Everything seems to visually look good and the tests seem to pass until the end when I get the following error messages:

request = <SubRequest '_django_db_marker' for <Function 'test_filter_recurring_outside_sync_window'>>

    @pytest.fixture(autouse=True)
    def _django_db_marker(request):
        """Implement the django_db marker, internal to pytest-django.

        This will dynamically request the ``db`` or ``transactional_db``
        fixtures as required by the django_db marker.
        """
        marker = request.keywords.get('django_db', None)
        if marker:
            validate_django_db(marker)
            if marker.transaction:
                getfixturevalue(request, 'transactional_db')
            else:
                getfixturevalue(request, 'db')

ve/lib/python2.7/site-packages/pytest_django/plugin.py:376:


self = <django.db.backends.sqlite3.base.SQLiteCursorWrapper object at 0x11976a478>
query = 'SELECT "django_migrations"."app", "django_migrations"."name" FROM "django_migrations"', params = ()

    def execute(self, query, params=None):
        if params is None:
            return Database.Cursor.execute(self, query)
        query = self.convert_query(query)
>       return Database.Cursor.execute(self, query, params)
E       OperationalError: no such table: django_migrations

ve/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py:485: OperationalError

I tried creating the table with ensure_schema in the conftest.py. I've tried every option of --nomigrations and --create-db to pytest.

I'm guessing it's a weird configuration problem I have with a legacy system, but I'm not sure where to start looking. Anyone have suggestions?

like image 995
Chet Avatar asked Jan 20 '17 18:01

Chet


1 Answers

It looks like may be an issue with migration.

Running ./manage.py schemamigration research --auto shows that most of the fields don't have any defaults specified.

Next, you then run ./manage.py schemamigration research --init followed by ./manage.py migrate research

This worked for me after I created the table:

python manage.py migrate --run-syncdb

NOTE: Don't forget to run python makemigrations first, i.e. python manage.py makemigrations {name of the app where patients model is}

Helpful tips: There is a table generated by django called django_migrations which keeps track of what migrations have been applied. If you delete your migrations, re-generate them and try to migrate without deleting the records from the table, then django will think it already applied them. You should never delete your migrations, as it will cause django to get confused.

You can skip the migrations step if you are actively developing If you are actively developing and want to skip the entire migrations system you can, but once you start using migrations, never delete them. Here is what I use while developing a new project:

dropdb mydb && createdb mydb && python manage.py migrate --run-syncdb && python manage.py loaddata initial

First, it drops the database and all data. Then it creates an empty one. The --run-syncdb generates the schema and the loaddata loads data from a fixtures file.

So, if you are still developing and can delete all your data and move what you care about to a fixtures file, then you can delete all your migration folders. You then can run the command above each time you change your model.

like image 132
bob marti Avatar answered Oct 22 '22 21:10

bob marti