Search code examples
pythondjangodjango-testingdjango-1.7django-migrations

How to create table during Django tests with managed = False?


From the oficial documentation:

For tests involving models with managed=False, it’s up to you to ensure the correct tables are created as part of the test setup.

I don't know how to create the tables as part of the test setup. I found this question and the accepted answer doesn't work for me. I think this is because the migrations files. The configuration is in the migrations files, to change the values "on the fly" don't have any effect.

What's the way to solve this in Django 1.7+?


Solution

  • I found a way. Modify the fixtures and add the SQL to generate the tables:

    #0001_initial.py (or followings)
    class Migration(migrations.Migration):
    
        operations = [
            migrations.RunSQL("CREATE TABLE..."),
            ...
        ]
    

    I'm a "migrations newbie" so I don't know if this is the best option. But it works.