簡體   English   中英

MySQL 錯誤 1146 和 1050 - 表不存在且表已存在

[英]MySQL Error 1146 and 1050 - Table does not exists and Table already exists

我一直在將我的數據庫從默認的 Django sqlite3 遷移到 MySql 但在運行此命令時 -

py manage.py migrate --run-syncdb

我收到以下錯誤

django.db.utils.ProgrammingError: (1146, "Table 'blogue_test.blogueapp_category' doesn't exist")

這就是我手動創建 SQL 表的方式

 CREATE TABLE blogueapp_category(
-> id int NOT NULL AUTO_INCREMENT,
-> name varchar(45) NOT NULL,
-> PRIMARY KEY (id)
-> );

然后重新運行相同的遷移命令,它顯示表已經存在?

MySQLdb._exceptions.OperationalError: (1050, "Table 'blogueapp_category' already exists")

這是我的 Django models.py

class Category(models.Model):
name = models.CharField(max_length=255, db_index=True)

def __str__(self):
    return self.name

def get_absolute_url(self):
    return reverse('category-list', kwargs={'cats': self.name})

編輯:運行“遷移”后出錯

Traceback (most recent call last):
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\mysql\base.py", line 73, in execute
return self.cursor.execute(query, args)
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\MySQLdb\cursors.py", line 206, in execute
res = self._query(query)
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\MySQLdb\cursors.py", line 319, in _query
db.query(q)
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\MySQLdb\connections.py", line 254, in query
_mysql.connection.query(self, query)
MySQLdb._exceptions.ProgrammingError: (1146, "Table 'blogue_test.blogueapp_category' doesn't exist")

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

Traceback (most recent call last):
  File "C:\Users\thesh\Desktop\Current Project\vert - Copy\blogue\manage.py", line 22, in <module>
main()
  File "C:\Users\thesh\Desktop\Current Project\vert - Copy\blogue\manage.py", line 18, in main
execute_from_command_line(sys.argv)
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
utility.execute()
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 398, in execute
output = self.handle(*args, **options)
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 89, in wrapped
res = handle_func(*args, **kwargs)
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\commands\migrate.py", line 75, in handle
self.check(databases=[database])
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 419, in check
all_issues = checks.run_checks(
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\checks\registry.py", line 76, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\resolvers.py", line 412, in check
for pattern in self.url_patterns:
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\resolvers.py", line 598, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\resolvers.py", line 591, in urlconf_module
return import_module(self.urlconf_name)
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "C:\Users\thesh\Desktop\Current Project\vert - Copy\blogue\blogue\urls.py", line 14, in <module>
path('', include('blogueApp.urls')),
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "C:\Users\thesh\Desktop\Current Project\vert - Copy\blogue\blogueApp\urls.py", line 3, in <module>
from .views import AddCommentView, HomeView, ArticleDetailView, AddPostView, UpdatePostView, DeletePostView, AddCategoryView, CategoryView, LikeView
  File "C:\Users\thesh\Desktop\Current Project\vert - Copy\blogue\blogueApp\views.py", line 5, in <module>
from .forms import EditForm, PostForm, CommentForm
  File "C:\Users\thesh\Desktop\Current Project\vert - Copy\blogue\blogueApp\forms.py", line 8, in <module>
for item in choices:
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\query.py", line 280, in __iter__
self._fetch_all()
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\query.py", line 1324, in _fetch_all
self._result_cache = list(self._iterable_class(self))
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\query.py", line 140, in __iter__
return compiler.results_iter(tuple_expected=True, chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\sql\compiler.py", line 1130, in results_iter
results = self.execute_sql(MULTI, chunked_fetch=chunked_fetch, chunk_size=chunk_size)
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\sql\compiler.py", line 1175, in execute_sql
cursor.execute(sql, params)
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 79, in _execute
with self.db.wrap_database_errors:
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\mysql\base.py", line 73, in execute
return self.cursor.execute(query, args)
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\MySQLdb\cursors.py", line 206, in execute
res = self._query(query)
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\MySQLdb\cursors.py", line 319, in _query
db.query(q)
  File "C:\Users\thesh\AppData\Local\Programs\Python\Python310\lib\site-packages\MySQLdb\connections.py", line 254, in query
_mysql.connection.query(self, query)

django.db.utils.ProgrammingError:(1146,“表'blogue_test.blogueapp_category'不存在”)

forms.py

from .models import Post, Category, Comment

choices = Category.objects.all().values_list('name', 'name')
choice_list = []

for item in choices:
    choice_list.append(item)

class PostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ('title', 'author', 'category', 'article_image','body')

        widgets = {
        'title': forms.TextInput(attrs={'class':'form-control', 'placeholder':'Enter Title'}),
        'author': forms.TextInput(attrs={'class':'form-control','value':'', 'id':'author_id', 'type':'hidden'}),
        'category': forms.Select(choices=choice_list, attrs={'class':'form-select'}),
        'body': forms.Textarea(attrs={'class':'form-control', 'placeholder':'Enter Main Content'}),
    }

我只是這方面的初學者,在過去的兩天里一直遇到同樣的錯誤,甚至嘗試在 PostgreSQL 中執行此操作,但結果相同。 任何幫助,將不勝感激。

您可以按照 2 個步驟遷移新表:

  1. 首先,刪除手動創建的表。

  2. 其次,在定義 model 時,請遵循 model 元類名中的 db_name:

     class Meta: db_table = 'x_claims_group_health'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM