python - 如何从 Django 中删除模型?

标签 python mysql django

我想从数据库中删除一些表。早些时候,我有像类(class)、部门这样的模型。现在,我想从数据库中删除所有这些并只使用一个:

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)

但是,当我使用时:

python manage.py makemigrations

它说 没有检测到变化。 并且在运行命令时:

python manage.py migrate

我正在将我所有的表放回数据库中,而这些表在 models.py 中不存在。我尝试使用:

  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

但没有成功。请帮我解决这个问题。

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'

最后在 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")

最佳答案

当我遇到同样的问题时,我是这样解决的:

  • 打开您的应用程序文件夹,然后打开迁移文件夹
  • 删除除init之外的所有文件
  • 尝试 makemigrations 并再次迁移

如果问题出在数据库中,而不是迁移中,这可能无济于事。

关于python - 如何从 Django 中删除模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37066671/

相关文章:

python - 在 Windows 上使用 NodeBox 运行图形库 + 无类型错误

Python (rospy) 到 C++ (roscpp) struct.unpack

python - Sqlalchemy 继承 - 删除子行而不删除父行?

mysql - Django - 如何从我想要从中获取对象的表中没有外键的表中获取值?

python - 构建一个可以根据其他 pd.DataFrame 功能导出新的哈希列的函数

php - 如何从 MySQL 记录发送自动电子邮件?

java - 打印 ASM 代码以生成给定的类

python - 概念。从 View 调用方法。错误处理

django - data._mutable= True 在 Django 休息框架

django - 创建 Heroku Django 应用程序后 syncdb 出现问题