Home > database >  python manage.py makemigrations says table already exists in Django project
python manage.py makemigrations says table already exists in Django project

Time:05-11

I am trying to get a coworker of mine up and running with a project I have already created. When we try to run the server it says one of the tables already exists. We googled it and tried to makemigrations and migrate --fake from posts like this Django : Table doesn't exist but still get the same result. Deleting the tables in the database isn't an option because its connecting to some production data. How can I get around this? The traceback is below.

Traceback (most recent call last):
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\django\db\backends\mysql\base.py", line 74, in execute
    return self.cursor.execute(query, args)
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\MySQLdb\cursors.py", line 209, in execute
    res = self._query(query)
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\MySQLdb\cursors.py", line 315, in _query
    db.query(q)
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\MySQLdb\connections.py", line 239, in query
    _mysql.connection.query(self, query)
MySQLdb._exceptions.ProgrammingError: (1146, "Table 'xpotoolsdb.xpotoolshome_site' doesn't exist")

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 "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\django\core\management\base.py", line 366, in execute
    self.check()
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\django\core\management\base.py", line 392, in check
    all_issues = self._run_checks(
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\django\core\management\commands\migrate.py", line 64, in _run_checks
    issues.extend(super()._run_checks(**kwargs))
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\django\core\management\base.py", line 382, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\django\core\checks\registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\django\core\checks\urls.py", line 40, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\django\core\checks\urls.py", line 57, in _load_all_namespaces
    url_patterns = getattr(resolver, 'url_patterns', [])
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\django\urls\resolvers.py", line 588, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\django\urls\resolvers.py", line 581, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Program Files\Python38\lib\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 848, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\xpotools\urls.py", line 25, in <module>
    path('', include(('xpotoolshome.urls', 'xpotoolshome'), namespace='xpotoolshome')),
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\django\urls\conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "C:\Program Files\Python38\lib\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 848, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\xpotoolshome\urls.py", line 7, in <module>
    from . import views
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\xpotoolshome\views.py", line 7, in <module>
    from .forms import EditProfileForm, EditProfileDetailForm, ProfileDetailForm, ProfileRegistrationForm, RegistrationForm, ManagerProfileDetailForm, UserPasswordChangeForm, ContactForm, ContactPhone, ContactEmail, BuidlingForm       
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\xpotoolshome\forms.py", line 37, in <module>
    class ProfileDetailForm(forms.ModelForm):
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\xpotoolshome\forms.py", line 38, in ProfileDetailForm
    drop_ware_list = getWarehouseList()
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\xpotoolshome\gxotools_functions.py", line 23, in getWarehouseList
    warehouse_list = ['Warehouse']   pd.DataFrame.from_records(Site.objects.filter(active=True).values('site')).sort_values('site')['site'].tolist()
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\pandas\core\frame.py", line 2222, in from_records
    arrays, arr_columns = to_arrays(data, columns)
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\pandas\core\internals\construction.py", line 829, in to_arrays
    if not len(data):
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\django\db\models\query.py", line 258, in __len__
    self._fetch_all()
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\django\db\models\query.py", line 1261, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\django\db\models\query.py", line 115, in __iter__
    for row in compiler.results_iter(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size):
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\django\db\models\sql\compiler.py", line 1103, in results_iter
    results = self.execute_sql(MULTI, chunked_fetch=chunked_fetch, chunk_size=chunk_size)
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\django\db\models\sql\compiler.py", line 1151, in execute_sql
    cursor.execute(sql, params)
 File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\django\db\backends\utils.py", line 100, in execute
    return super().execute(sql, params)
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\django\db\backends\mysql\base.py", line 74, in execute
    return self.cursor.execute(query, args)
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\MySQLdb\cursors.py", line 209, in execute
    res = self._query(query)
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\MySQLdb\cursors.py", line 315, in _query
    db.query(q)
  File "C:\Users\tmartinez005\GIT\GXOTools.com-xpotools\gxotools\lib\site-packages\MySQLdb\connections.py", line 239, in query
    _mysql.connection.query(self, query)
django.db.utils.ProgrammingError: (1146, "Table 'xpotoolsdb.xpotoolshome_site' doesn't exist")

CodePudding user response:

first delete all migrations files on your apps then delete database and create it again

then run

python3 manage.py makemigrations

and then:

python3 manage.py migrate

if it did not work show your model in this question

CodePudding user response:

To prevent this from happening in the future, it is worth to remember: Django migration files should be considered as part of the codebase, only deleted when moving from south migration into django.core.migrations . reset south migration in production is the following: delete migrations make sure model, either deleted or commented out.

python manage.py makemigrations
python manage.py migrate --fake-initial 

also try :

python manage.py makemigrations
python manage.py migrate --fake APPNAME zero 

But in your case, the application is try to access the database on import which can be very tricky to fix, to further debug this, kindly share models.py

  • Related