2

I have quite a specific problem, with django-admin.py not creating tables for model in the application directory. I have created a PostgreSQL database, and user, then granted this user all Privileges on the database, then included this database in settings.py. The relevant section of settings.py looks like below:

DATABASES = {
    'default': {
    'ENGINE':    'django.db.backends.postgresql_psycopg2',
    'NAME':      'crawler',
    'USER':      'austinpowers',
    'PASSWORD':  '****',
    'HOST':      'localhost',
    'PORT':      ''
    }
}

Django is able to access this database, as manage.py dbshell works well.

Now i want Django to create tables based on following models.py file of application "crawler":

from django.db import models


class Files(models.Model):
    id = models.IntegerField(primary_key=True, blank=False, null=False)  
    filename = models.TextField(blank=True, null=True)
    status = models.IntegerField(blank=True, null=True)

    def __unicode__(self):
        return self.filename

    class Meta:
        verbose_name = 'File'
        verbose_name_plural = 'Files'


class IpLog(models.Model):
    id = models.IntegerField(primary_key=True, blank=False, null=False)  
    ip = models.TextField(blank=True, null=True)
    time_changed = models.TextField(blank=True, null=True)

    def __unicode__(self):
        return self.time_changed

    class Meta:
        verbose_name = 'IP Log'
        verbose_name_plural = 'IP Logs'


class Jobs(models.Model):
    id = models.IntegerField(primary_key=True, blank=False, null=False)  
    run_no = models.IntegerField(blank=True, null=True)
    shop = models.TextField(blank=True, null=True)
    pzn = models.TextField(blank=True, null=True)
    status = models.IntegerField(blank=True, null=True)

    def __unicode__(self):
        return self.shop + ": " + self.pzn

    class Meta:
    verbose_name_plural = 'Jobs'
    verbose_name = 'Job'

So I make sure "migrations" folder in "crawler" app subdirectory is empty, the database is empty, and then call:

python manage.py makemigrations

.. what leads to following output:

No changes detected

So I want to force Django to create initial migrations for application "crawler" by running:

python manage.py makemigrations crawler --empty

... what outputs:

Migrations for 'crawler':
  crawler/migrations/0001_initial.py:

However, when I want to see the contents of the migration file /crawler/migrations/0001_initial.py, I get an empty migration file:

# -*- coding: utf-8 -*-
# Generated by Django 1.10.4 on 2016-12-15 13:25
from __future__ import unicode_literals

    from django.db import migrations


    class Migration(migrations.Migration):

        dependencies = [
        ]

        operations = [
        ]

What suggests, that Django does not want for some reason, to create corresponding tables for models in models.py.

Indeed, when i call ...

python manage.py migrate

... I get the following output:

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, crawler, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying crawler.0001_initial... OK

And the tables for my models are not present in the database.

Do you know, how can I force Django to create the tables? Django version used is 1.10.4

Thanks in advance for your help.

8
  • Is your crawler app in the list of INSTALLED_APPS? Commented Dec 15, 2016 at 13:33
  • On top of what @chris pointed out, use command python manage.py migrate to apply migration changes to db. Commented Dec 15, 2016 at 13:35
  • @VikashSingh: Isn't OP already doing that? Commented Dec 15, 2016 at 13:36
  • @Chris Yes it is. Commented Dec 15, 2016 at 13:37
  • @VikashSingh as I have written above, I call this command. Commented Dec 15, 2016 at 13:37

1 Answer 1

3

You are mixing up data migrations and schema migrations. The --empty flag is used in data migrations specifically to create an empty migration file, so that you can put your custom migration commands to produce a data migration.

In your case you don't have data. So the correct command is

 python manage.py makemigrations crawler 

Followed by

 python manage.py migrate

Update: Note that the very first time you run migrations on a newly created app, you need to explicitly mention the app name. Merely doing ./manage.py makemigrations often does not work. Which is what happened the first time you ran it without the app name.

Sign up to request clarification or add additional context in comments.

6 Comments

As OP has mentioned: python manage.py makemigrations leads to No changes detected?
@AKS, the OPs command was python manage.py makemigrations --empty
Is it so? Please check again from top ;-)
@e4c5 Yes, update is good enough :). Though I would like to know why it often doesn't work on new apps?
@AKS, that part I never figure out either!! :-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.