简体   繁体   中英

Make migrations error in django: "no such table"

when I was trying to make migrations: python manage.py makemigrations

The following error occurred: django.db.utils.OperationalError: no such table: blog_category

Traceback (most recent call last):
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django \db\backends\sqlite3\base.py", line 383, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: blog_category

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

Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django \core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 361, in execute
self.check()
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 390, in check
include_deployment_checks=include_deployment_checks,
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 377, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\checks\registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\checks\urls.py", line 40, in check_url_namespaces_unique
all_namespaces = _load_all_namespaces(resolver)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\checks\urls.py", line 57, in _load_all_namespaces
url_patterns = getattr(resolver, 'url_patterns', [])
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\urls\resolvers.py", line 571, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\urls\resolvers.py", line 564, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\importlib\__init__.py",   line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\xampp\htdocs\Django project\blooper\Bona-Blog-master\bona_blog\urls.py", line 27, in <module>
path('', include('blog.urls', namespace='blog')),  # Urls for article app.
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\xampp\htdocs\Django project\blooper\Bona-Blog-master\blog\urls.py", line 5, in <module>
from blog.views.blog.article_views import (
File "C:\xampp\htdocs\Django project\blooper\Bona-Blog-master\blog\views\blog\article_views.py", line 25, in <module>
from blog.forms.blog.article_forms import (
File "C:\xampp\htdocs\Django project\blooper\Bona-Blog-master\blog\forms\blog\article_forms.py", line 16, in <module>
class ArticleCreateForm(forms.ModelForm):
File "C:\xampp\htdocs\Django project\blooper\Bona-Blog-master\blog\forms\blog\article_forms.py", line 18, in ArticleCreateForm
class Meta:
File "C:\xampp\htdocs\Django project\blooper\Bona-Blog-master\blog\forms\blog\article_forms.py", line 47, in Meta
"data-live-search": "true"
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\forms\widgets.py", line 562, in __init__
self.choices = list(choices)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\query.py", line 274, in __iter__
self._fetch_all()
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\query.py", line 1242, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\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 "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\sql\compiler.py", line 1097, in execute_sql
cursor.execute(sql, params)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 99, in execute
return super().execute(sql, params)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\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: blog_category

Please Help How to remove this error? I'm new in Django and I had clone this project from Github https://github.com/Williano/Bona-Blog

using Django-2.2

Apparantly,

  1. Either you have changed your db tables manually (eg via db interface)

  2. You are missing one or more of required migration files.

As you are on a development environment I would suggest

  • drop the db
  • re-create the db
  • then python manage.py migrate again

This seams to be a bug in the blog software.

When the migrations are created the models in the code are introspected and in this process many modules are imported with the models. In the software there is a point where a query is made at import time ( article_forms.py , line 40 . At this point the table the query points to does not exist yet and it fails.

It is possible the author did not notice the bug because he/she had the table already when doing the change. And he/she never created a new instance from scratch.

You should report it as a bug.

Since you are in development stage you can drop the db and recreate it. But before recreating delete all the previous migration files on all the apps. Make sure that there are only lnlt files in those folders

  • python manage.py makemigrations
  • python manage.py migrate

This fixed it for me!

This question is old but I am putting a solution that worked for me as no one else has mentioned it yet. Also this could have just been a comment but I need 50 reputations to pull a comment, which I do not have at the time of writing this.

Instead of running project-wide migrations, make it specific to the blog app:

python manage.py makemigrations blog
python manage.py migrate blog

Do this for your other apps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM