[英]How to delete models from django?
I want to delete some tables from database. 我想从数据库中删除一些表。 Earlier, I had models like course, department.
之前,我有课程,部门等模型。 Now, I want to delete all of them from database and use only one:
现在,我想从数据库中删除所有它们,并仅使用其中一个:
class Student(models.Model):
"""here goes model for users"""
def __str__(self):
return self.name
name = models.CharField(default = "",max_length=200)
enrollment_no = models.CharField(default = "",max_length=10)
batch = models.CharField(default = "",max_length=4)
father_income = models.IntegerField(max_length=100)
email = models.CharField(default = "",max_length=1000)
mobile_number = models.CharField(default = "",max_length=1000)
But, when I am using: 但是,当我使用时:
python manage.py makemigrations
It says no changes detected.
它说
no changes detected.
And on running command: 并在运行命令:
python manage.py migrate
I am getting all my tables back in the database, while those table doesn't exist in models.py. 我将所有表恢复到数据库中,而这些表在models.py中不存在。 I tried to use:
我尝试使用:
382 python manage.py makemigrations
383 python manage.py squashmigrations
384 python manage.py squashmigrations appname
385 python manage.py squashmigrations appname 001
386 python manage.py squashmigrations appname 0001
387 python manage.py makemigrations
388 python manage.py migrate
389 python manage.py Reporter.objects.all().delete()
390 Reporter.objects.all().delete()
391 python manage.py sqlclear appname | python manage.py dbshell
392 python manage.py sqlclear appname
393 ./manage.py sqlclear appname | ./manage.py dbshell
394 python manage.py sqlclear
395 python manage.py flush
396 python manage.py syncdb
397 python manage.py makemigrations
398 python manage.py migrate
399 python manage.py makemigrations
400 python manage.py migrate
401 python manage.py makemigrations
402 python manage.py flush
403 python manage.py makemigrations
404 python manage.py migrate
405 python manage.py makemigrations
406 python manage.py migrate
407 python manage.py makemigrations
408 python manage.py migrate
409 python manage.py sqlflush
410 python manage.py sqlflush | python manage.py dbshell
411 python manage.py sqlclear jmialumniusa_app
412 python manage.py help flush
413 python manage.py flush
414 python manage.py makemigrations
415 python manage.py migrate
416 python manage.py schemamigration --auto appname/
417 python manage.py schemamigration --auto appname
418 python manage.py makemigrations
419 history
but getting no success. 但没有成功。 Please help me to solve this.
请帮我解决这个问题。
The output of python manage.py makemigrations jmialumniusa_app
is: python manage.py makemigrations jmialumniusa_app
的输出为:
System check identified some issues:
WARNINGS:
?: (urls.W001) Your URL pattern '^$' uses include with a regex ending with a '$'. Remove the dollar from the regex to avoid problems including URLs.
No changes detected in app 'jmialumniusa_app'
And finally on python manage.py migrate
I am getting: 最后在
python manage.py migrate
我得到:
System check identified some issues:
WARNINGS:
?: (urls.W001) Your URL pattern '^$' uses include with a regex ending with a '$'. Remove the dollar from the regex to avoid problems including URLs.
Operations to perform:
Apply all migrations: admin, contenttypes, sessions, auth, jmialumniusa_app
Running migrations:
Rendering model states... DONE
Applying jmialumniusa_app.0003_auto_20160506_0654...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 200, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 92, in migrate
self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 198, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py", line 123, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/fields.py", line 121, in database_forwards
schema_editor.remove_field(from_model, from_model._meta.get_field(self.name))
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 438, in remove_field
self.execute(sql)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 110, in execute
cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 112, in execute
return self.cursor.execute(query, args)
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
django.db.utils.OperationalError: (1091, "Can't DROP 'student_id'; check that column/key exists")
after deleting your unwanted models from models.py, try this: 从models.py删除不需要的模型后,请尝试以下操作:
python manage.py makemigrations your_app
python manage.py migrate
reason can be that your app might not have been yet "registered" to have migrations 原因可能是您的应用可能尚未“注册”进行迁移
The first answer should work for you. 第一个答案应该为您工作。 Another thing you can do is delete the migrations folder in the app and run
python manage.py makemigrations app_name
您可以做的另一件事是删除应用程序中的migrations文件夹,然后运行
python manage.py makemigrations app_name
A new migrations folder will be automatically created. 新的迁移文件夹将自动创建。 then you can migrate
那么你可以迁移
python manage.py migrate
You should be good after this. python manage.py migrate
你应该在这之后好。 I DO NOT ENCOURAGE THIS but as a last resort, if you are using sqlite, just delete it and do the what I stated above. 我不鼓励这样做,但万不得已,如果您使用的是sqlite,请将其删除并执行我上面所述的操作。
https://docs.djangoproject.com/en/1.9/topics/migrations/ will give you more insights on how migrations work in django. https://docs.djangoproject.com/zh-CN/1.9/topics/migrations/将为您提供有关django中迁移工作方式的更多见解。
When I had same issue, I fixed it by this way: 当我遇到相同的问题时,可以通过以下方式解决它:
It may not help if the problem is in DB, not in migrations. 如果问题出在数据库中,而不是迁移中,这可能无济于事。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.