Oleg Belousov
Oleg Belousov

Reputation: 10121

Cannot create DB from models (makemigrations) after deleting DB and migrations

I have dropped the entire db and recreated it and also removed the pycache folders from the app and the root folder of the project (as well as the migration folder).

I am trying to create the database structure from the models as following: $ python manage.py makemigrations

This the error I am getting, and unfortunately nothing so far helped me:

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/olegbelousov/.virtualenvs/photobasa/lib/python3.5/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/Users/olegbelousov/.virtualenvs/photobasa/lib/python3.5/site-packages/django/core/management/__init__.py", line 327, in execute
    django.setup()
  File "/Users/olegbelousov/.virtualenvs/photobasa/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/olegbelousov/.virtualenvs/photobasa/lib/python3.5/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/Users/olegbelousov/.virtualenvs/photobasa/lib/python3.5/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/Users/olegbelousov/.virtualenvs/photobasa/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 662, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/Users/olegbelousov/dev/photobase/proto/models.py", line 100, in <module>
    class Photo(TimeStampedModel, SoftDeletableModel):
  File "/Users/olegbelousov/dev/photobase/proto/models.py", line 101, in Photo
    PHOTOGRAPHER_COUNT = Photographer.objects.count()
  File "/Users/olegbelousov/.virtualenvs/photobasa/lib/python3.5/site-packages/django/db/models/manager.py", line 122, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/olegbelousov/.virtualenvs/photobasa/lib/python3.5/site-packages/django/db/models/query.py", line 371, in count
    return self.query.get_count(using=self.db)
  File "/Users/olegbelousov/.virtualenvs/photobasa/lib/python3.5/site-packages/django/db/models/sql/query.py", line 483, in get_count
    number = obj.get_aggregation(using, ['__count'])['__count']
  File "/Users/olegbelousov/.virtualenvs/photobasa/lib/python3.5/site-packages/django/db/models/sql/query.py", line 464, in get_aggregation
    result = compiler.execute_sql(SINGLE)
  File "/Users/olegbelousov/.virtualenvs/photobasa/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 848, in execute_sql
    cursor.execute(sql, params)
  File "/Users/olegbelousov/.virtualenvs/photobasa/lib/python3.5/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/Users/olegbelousov/.virtualenvs/photobasa/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Users/olegbelousov/.virtualenvs/photobasa/lib/python3.5/site-packages/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Users/olegbelousov/.virtualenvs/photobasa/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/Users/olegbelousov/.virtualenvs/photobasa/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "proto_photographer" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "proto_photographer"
                                          ^

Upvotes: 0

Views: 186

Answers (1)

Ian Price
Ian Price

Reputation: 7616

makemigrations inspects the db to see if changes have occured since the last migration for each app in your project. In order for this to function, your Django project must be able to successfully start running. If there are prior tables missing, this won't happen.

Run python manage.py migrate to run all existing migrations and get your database to the state stored by your last set of migrations. Afterwards, you should be able to run python manage.py makemigrations to create new migrations and then apply them with python manage.py migrate once again.

Upvotes: 2

Related Questions