Django doesn't create any tables in Postgres database

Question:

I have an app with an initial data fixture on Django 1.8. When I run manage.py migrate, I get the following output:

Operations to perform:
  Synchronize unmigrated apps: food, locations, messages, staticfiles, core
  Apply all migrations: auth, sessions, admin, contenttypes
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
/home/myuser/venv/lib/python3.4/site-packages/django/core/management/commands/loaddata.py:229: RemovedInDjango19Warning: initial_data fixtures are deprecated. Use data migrations instead.
  RemovedInDjango19Warning

Installed 87 object(s) from 1 fixture(s)
Running migrations:
  No migrations to apply.

So it seems like the fixture was installed, but not really – when I log in to my postgres user and run psql and dt, no tables are listed (I make sure to connect to the database first with c mydatabase)! When trying to view the app in a browser, I get: ProgrammingError at / relation "locations_openinghours" does not exist. (Which just happens to be the first table that the app tries to access).

Asked By: tao_oat

||

Answers:

I solved it, but it was a silly issue. First, when running python manage.py makemigrations for the first time, you should specify which app you’re migrating for – so I used python manage.py makemigrations myapp. Secondly, the way I’ve set up my project, manage.py uses the dev settings by default, and this was writing to an SQLite database. I added the --settings=core.settings.production flag, and it worked.

Answered By: tao_oat

You can try these steps to see if it resolves your problem:

  1. delete all the migration files for your app, as well as the database records for the app migrations in the django_migrations table.
  2. Run: python manage.py makemigrations <appname>
  3. Run: python manage.py migrate <appname>

In addition to the above you can try adding to each of your model classes the meta class setting managed = True and app_label = <name of your app>

i.e.

class Food(models.Model):
class Meta:
app_label = "food"
managed = True

The rest of your class as you had it. Of course add this Meta for all your models. Make sure the app_label is lower case like you have it in the INSTALLED_APPS setting.

Answered By: Softinio

In my case those solutions didn’t work to me

I have to:

  1. Create a new app: py manage.py startapp new_app_name
  2. Copy all the files in my new app
  3. Run: py manage.py makemigrations
  4. Run: py manage.py migrate

It works for me, I hope it could be useful for you too

Answered By: Harrison Hoyos
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.