简体   繁体   English

使用迁移创建新表时,错误表已存在

[英]Error table already exists when using migrate to create new table

When I create a new model inside models.py and run the following: 当我在models.py中创建新模型并运行以下命令时:

python manage.py makemigrations appname #worked
python manage.py migrate #doesnt worked

I get this error: 我收到此错误:

table "storage_OldModel" already exists 表“ storage_OldModel”已存在

I also checked this question Django migrate : doesn't create tables but there wasn't anything to help me out. 我还检查了这个问题Django migration:不创建表,但是没有任何帮助。

This is the error trace: 这是错误跟踪:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:9000/ad/storage/pcmodels/

Django Version: 2.1
Python Version: 3.7.0
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'customers.apps.CustomersConfig',
 'storage.apps.StorageConfig',
 'notes.apps.NotesConfig',
 'search.apps.SearchConfig',
 'crispy_forms',
 'widget_tweaks',
 'import_export',
 'formset_bootstrap']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "D:\coding\project\lib\site-packages\django\db\backends\utils.py" in _execute
  85.                 return self.cursor.execute(sql, params)

File "D:\coding\project\lib\site-packages\django\db\backends\sqlite3\base.py" in execute
  296.         return Database.Cursor.execute(self, query, params)

The above exception (no such table: storage_pcmodels) was the direct cause of the following exception:

File "D:\coding\project\lib\site-packages\django\core\handlers\exception.py" in inner
  34.             response = get_response(request)

File "D:\coding\project\lib\site-packages\django\core\handlers\base.py" in _get_response
  126.                 response = self.process_exception_by_middleware(e, request)

File "D:\coding\project\lib\site-packages\django\core\handlers\base.py" in _get_response
  124.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "D:\coding\project\lib\site-packages\django\contrib\admin\options.py" in wrapper
  607.                 return self.admin_site.admin_view(view)(*args, **kwargs)

File "D:\coding\project\lib\site-packages\django\utils\decorators.py" in _wrapped_view
  142.                     response = view_func(request, *args, **kwargs)

File "D:\coding\project\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
  44.         response = view_func(request, *args, **kwargs)

File "D:\coding\project\lib\site-packages\django\contrib\admin\sites.py" in inner
  223.             return view(request, *args, **kwargs)

File "D:\coding\project\lib\site-packages\django\utils\decorators.py" in _wrapper
  45.         return bound_method(*args, **kwargs)

File "D:\coding\project\lib\site-packages\django\utils\decorators.py" in _wrapped_view
  142.                     response = view_func(request, *args, **kwargs)

File "D:\coding\project\lib\site-packages\django\contrib\admin\options.py" in changelist_view
  1685.             cl = self.get_changelist_instance(request)

File "D:\coding\project\lib\site-packages\django\contrib\admin\options.py" in get_changelist_instance
  745.             sortable_by,

File "D:\coding\project\lib\site-packages\django\contrib\admin\views\main.py" in __init__
  81.         self.get_results(request)

File "D:\coding\project\lib\site-packages\django\contrib\admin\views\main.py" in get_results
  209.         result_count = paginator.count

File "D:\coding\project\lib\site-packages\django\utils\functional.py" in __get__
  37.         res = instance.__dict__[self.name] = self.func(instance)

File "D:\coding\project\lib\site-packages\django\core\paginator.py" in count
  87.             return self.object_list.count()

File "D:\coding\project\lib\site-packages\django\db\models\query.py" in count
  383.         return self.query.get_count(using=self.db)

File "D:\coding\project\lib\site-packages\django\db\models\sql\query.py" in get_count
  498.         number = obj.get_aggregation(using, ['__count'])['__count']

File "D:\coding\project\lib\site-packages\django\db\models\sql\query.py" in get_aggregation
  483.         result = compiler.execute_sql(SINGLE)

File "D:\coding\project\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
  1061.             cursor.execute(sql, params)

File "D:\coding\project\lib\site-packages\django\db\backends\utils.py" in execute
  100.             return super().execute(sql, params)

File "D:\coding\project\lib\site-packages\django\db\backends\utils.py" in execute
  68.         return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)

File "D:\coding\project\lib\site-packages\django\db\backends\utils.py" in _execute_with_wrappers
  77.         return executor(sql, params, many, context)

File "D:\coding\project\lib\site-packages\django\db\backends\utils.py" in _execute
  85.                 return self.cursor.execute(sql, params)

File "D:\coding\project\lib\site-packages\django\db\utils.py" in __exit__
  89.                 raise dj_exc_value.with_traceback(traceback) from exc_value

File "D:\coding\project\lib\site-packages\django\db\backends\utils.py" in _execute
  85.                 return self.cursor.execute(sql, params)

File "D:\coding\project\lib\site-packages\django\db\backends\sqlite3\base.py" in execute
  296.         return Database.Cursor.execute(self, query, params)

Exception Type: OperationalError at /ad/storage/pcmodels/
Exception Value: no such table: storage_pcmodels

If someone knows a solution please let me know. 如果有人知道解决方案,请告诉我。

as i used django 2.2 version the solution is 当我使用django 2.2版本时,解决方案是

python manage.py makemigration app_name

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

hope it will help you 希望对你有帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当表已存在时,使用 spark 数据帧覆盖表失败 - Overwriting Table using spark dataframe fails when table already exists MySQLdb Python-如果不存在则使用CREATE TABLE时仍然出现错误 - MySQLdb Python - Still getting error when using CREATE TABLE IF NOT EXISTS “如何使用CREATE方法修复python IDE中sql中已经存在的错误表?” - “How to fix the error table already exists in sql in a python IDE using CREATE method?” MySQL 错误 1146 和 1050 - 表不存在且表已存在 - MySQL Error 1146 and 1050 - Table does not exists and Table already exists SQL Alchemy,pymssql,Pandas 0.24.2 to_sql 试图在表已经存在时创建表 - SQL Alchemy, pymssql, Pandas 0.24.2 to_sql Trying to create table when table already exists Django 1.6.5中的Syncdb命令不会在models.py中创建新的模型类-说表已经存在 - Syncdb command in Django 1.6.5 does not create new model class in models.py - says table already exists SQLAlchemy:表已经存在 - SQLAlchemy: Table already exists Django 在现有表中创建一个新列 - Django create a new column in an already existing table Django&South:添加新字段但发生DatabaseError“表已存在” - Django & South: Adding new field but DatabaseError occurs “table already exists” 在两个Django项目之间共享数据库时如何解决“表“auth_permission”已经存在”错误 - How to solve "table "auth_permission" already exists" error when the database is shared among two Django projects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM