gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 00:19:23 32 4
gpt4 key购买 nike

我想从数据库中删除一些表。早些时候,我有像类(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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com