简体   繁体   中英

MySQL Error 1146 and 1050 - Table does not exists and Table already exists

I've been migrating my database from the default Django sqlite3 to MySql but while running this command -

py manage.py migrate --run-syncdb

I get the following error

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

This is how I create the SQL Table manually

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

Then re-run the same migrate command and it shows me the table already exists?

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

This is my 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})

EDIT: Error after running 'migrate'

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, "Table 'blogue_test.blogueapp_category' doesn't exist")

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'}),
    }

I am merely a beginner in this and have been stuck with this same error for the past 2 days, even tried doing it in PostgreSQL but resulted in the same. Any help would be appreciated.

2 steps you can follow to migrate fresh table:

  1. First, delete the manually created table.

  2. Second, while defining the model please follow the db_name in your meta classname of model:

     class Meta: db_table = 'x_claims_group_health'

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