Forums

Django Migrations

Hi! I accidentally deleted my db.sqlite3 file, and so I was trying to redo the migrations. I deleted everything in all of the migrations directories except the init files. When I run "python3 manage.py makemigrations", I am getting "django.db.utils.OperationalError: no such table:" and then a table name. What am I doing wrong? Thanks!!

You should have run migrate management command after recreating migrations (in fact deleting the migrations was not necessary after your db perished). Basically makemigrations command "is responsible for creating new migrations based on the changes you have made to your models" and migrate "is responsible for applying and unapplying migrations" as Django docs put it.

Thanks for the response! So since I did delete the migrations, wouldn't I recreate them with makemigrations ? When I try to make migrations its telling me that there is no such table. How do I recreate this table?

It looks like you might still have __pycache__ directories in your migrations directories.

I just deleted them, but I am still getting the same error. Should I also delete the pycache directories outside of the migrations directories?

Don't use makemigrations. First use migrate to create your base database from the models as they currently are. If that still gives an error, post the entire traceback so we can have a better idea of what is happening.

Ok, its super long but here it is:

  Traceback (most recent call last):                                                                      
  File "/usr/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute             
    return self.cursor.execute(sql, params)                                                             
  File "/usr/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute      
    return Database.Cursor.execute(self, query, params)                                                 
  sqlite3.OperationalError: no such table: APP_post

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

Traceback (most recent call last):                                                                      
  File "manage.py", line 21, in <module>                                                                
    main()                                                                                              
  File "manage.py", line 17, in main                                                                    
    execute_from_command_line(sys.argv)                                                                 
  File "/usr/lib/python3.8/site-packages/django/core/management/__init__.py", line 381, in execute_from_
command_line                                                                                            
    utility.execute()                                                                                   
  File "/usr/lib/python3.8/site-packages/django/core/management/__init__.py", line 375, in execute      
    self.fetch_command(subcommand).run_from_argv(self.argv)                                             
  File "/usr/lib/python3.8/site-packages/django/core/management/base.py", line 323, in run_from_argv    
    self.execute(*args, **cmd_options)                                                                  
  File "/usr/lib/python3.8/site-packages/django/core/management/base.py", line 361, in execute          
    self.check()                                                                                        
  File "/usr/lib/python3.8/site-packages/django/core/management/base.py", line 387, in check            
    all_issues = self._run_checks(                                                                      
  File "/usr/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 65, in _run_c
hecks                                                                                                   
    issues.extend(super()._run_checks(**kwargs))                                                        
  File "/usr/lib/python3.8/site-packages/django/core/management/base.py", line 377, in _run_checks      
    return checks.run_checks(**kwargs)                                                                  
  File "/usr/lib/python3.8/site-packages/django/core/checks/registry.py", line 72, in run_checks        
    new_errors = check(app_configs=app_configs)                                                         
  File "/usr/lib/python3.8/site-packages/django/core/checks/urls.py", line 13, in check_url_config      
    return check_resolver(resolver)                                                                     
  File "/usr/lib/python3.8/site-packages/django/core/checks/urls.py", line 23, in check_resolver 
return check_method()                                                                               
  File "/usr/lib/python3.8/site-packages/django/urls/resolvers.py", line 399, in check
    for pattern in self.url_patterns:
  File "/usr/lib/python3.8/site-packages/django/utils/functional.py", line 80, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/lib/python3.8/site-packages/django/urls/resolvers.py", line 584, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/usr/lib/python3.8/site-packages/django/utils/functional.py", line 80, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/lib/python3.8/site-packages/django/urls/resolvers.py", line 577, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/ConnorCollins/congressionalApp/congressionalApp/urls.py", line 12, in <module>
    path('', include('APP.urls')),
  File "/usr/lib/python3.8/site-packages/django/urls/conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
12:12 ~/congressionalApp $ python3 manage.py migrate                                                    
Traceback (most recent call last):                                                                      
  File "/usr/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute             
    return self.cursor.execute(sql, params)                                                             
  File "/usr/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute      
    return Database.Cursor.execute(self, query, params)                                                 
sqlite3.OperationalError: no such table: APP_post

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

Traceback (most recent call last):
File "manage.py", line 21, in <module>                                                                
    main()                                                                                              
  File "manage.py", line 17, in main                                                                    
    execute_from_command_line(sys.argv)                                                                 
  File "/usr/lib/python3.8/site-packages/django/core/management/__init__.py", line 381, in execute_from_
command_line                                                                                            
    utility.execute()                                                                                   
  File "/usr/lib/python3.8/site-packages/django/core/management/__init__.py", line 375, in execute      
    self.fetch_command(subcommand).run_from_argv(self.argv)                                             
  File "/usr/lib/python3.8/site-packages/django/core/management/base.py", line 323, in run_from_argv    
    self.execute(*args, **cmd_options)                                                                  
  File "/usr/lib/python3.8/site-packages/django/core/management/base.py", line 361, in execute          
    self.check()                                                                                        
  File "/usr/lib/python3.8/site-packages/django/core/management/base.py", line 387, in check            
    all_issues = self._run_checks(                                                                      
  File "/usr/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 65, in _run_c
hecks                                                                                                   
    issues.extend(super()._run_checks(**kwargs))                                                        
  File "/usr/lib/python3.8/site-packages/django/core/management/base.py", line 377, in _run_checks      
    return checks.run_checks(**kwargs)                                                                  
  File "/usr/lib/python3.8/site-packages/django/core/checks/registry.py", line 72, in run_checks        
    new_errors = check(app_configs=app_configs)                                                         
  File "/usr/lib/python3.8/site-packages/django/core/checks/urls.py", line 13, in check_url_config      
    return check_resolver(resolver)                                                                     
  File "/usr/lib/python3.8/site-packages/django/core/checks/urls.py", line 23, in check_resolver        
    return check_method()                                                                               
  File "/usr/lib/python3.8/site-packages/django/urls/resolvers.py", line 399, in check
    for pattern in self.url_patterns:
  File "/usr/lib/python3.8/site-packages/django/utils/functional.py", line 80, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/lib/python3.8/site-packages/django/urls/resolvers.py", line 584, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/usr/lib/python3.8/site-packages/django/utils/functional.py", line 80, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/lib/python3.8/site-packages/django/urls/resolvers.py", line 577, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/ConnorCollins/congressionalApp/congressionalApp/urls.py", line 12, in <module>
    path('', include('APP.urls')),
  File "/usr/lib/python3.8/site-packages/django/urls/conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/ConnorCollins/congressionalApp/APP/urls.py", line 2, in <module>
    from . import views
  File "/home/ConnorCollins/congressionalApp/APP/views.py", line 49, in <module>
    class PostListView(ListView):
  File "/home/ConnorCollins/congressionalApp/APP/views.py", line 50, in PostListView
    for post in Post.objects.all():
  File "/usr/lib/python3.8/site-packages/django/db/models/query.py", line 274, in __iter__
    self._fetch_all()
  File "/usr/lib/python3.8/site-packages/django/db/models/query.py", line 1242, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/usr/lib/python3.8/site-packages/django/db/models/query.py", line 55, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/usr/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1100, in execute_sql
    cursor.execute(sql, params)
  File "/usr/lib/python3.8/site-packages/django/db/backends/utils.py", line 99, in execute
    return super().execute(sql, params)
  File "/usr/lib/python3.8/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/usr/lib/python3.8/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrapper
    return executor(sql, params, many, context)
  File "/usr/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/usr/lib/python3.8/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/usr/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: APP_post

Are you sure you're using an entirely new database and not one that has remnants of the previous database in it?

Yes. I think it completely deleted the database when I accidentally deleted the database file.

Ah! From this part of the traceback:

File "/home/ConnorCollins/congressionalApp/APP/views.py", line 49, in <module>    class PostListView(ListView):
File "/home/ConnorCollins/congressionalApp/APP/views.py", line 50, in PostListView
for post in Post.objects.all():

It looks like you're trying to access the database at import time in line 50 in /home/ConnorCollins/congressionalApp/APP/views.py. You can't do that, because the database does not exist yet.

I commented that out and it worked ! Thanks so much for your help!!!!!!!!

Excellent!