获取我要删除的模型的Django模型错误

时间:2019-01-14 20:41:39

标签: django

你能帮忙吗?

我在Django models.py上犯了一个错误,想删除该模型。

从下面可以看到,迁移是删除称为代码的模型。

但是,它仍然会出错并且不会删除模型。

我该怎么办?

root@new:/home/django/django_project# python manage.py makemigrations
Migrations for 'netshock':
  netshock/migrations/0018_delete_code.py
    - Delete model code
root@new:/home/django/django_project# python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, netshock, sessions
Running migrations:
  Applying netshock.0012_code...Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/lib/python2.7/dist-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/usr/lib/python2.7/dist-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/lib/python2.7/dist-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/lib/python2.7/dist-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/usr/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "/usr/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/usr/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/usr/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/usr/lib/python2.7/dist-packages/django/db/migrations/migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/usr/lib/python2.7/dist-packages/django/db/migrations/operations/models.py", line 97, in database_forwards
    schema_editor.create_model(model)
  File "/usr/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 303, in create_model
    self.execute(sql, params or None)
  File "/usr/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 120, in execute
    cursor.execute(sql, params)
  File "/usr/lib/python2.7/dist-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/usr/lib/python2.7/dist-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/usr/lib/python2.7/dist-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/lib/python2.7/dist-packages/django/db/backends/utils.py", line 62, in execute
    return self.cursor.execute(sql)
django.db.utils.ProgrammingError: syntax error at or near "4000000000"
LINE 1: ...("id" serial NOT NULL PRIMARY KEY, "code" varchar(4000000000...

1 个答案:

答案 0 :(得分:1)

问题在错误的底部指定:

django.db.utils.ProgrammingError: syntax error at or near "4000000000"
LINE 1: ...("id" serial NOT NULL PRIMARY KEY, "code" varchar(4000000000...

现在4000000000太大了。 PostgreSQL提到maximum size of character types is 1GB [PostgreSQL-doc]

  

(...)在任何情况下,可能的最长字符串   存储的空间约为 1 GB 。 (将允许的最大值   数据类型声明中的 n 小于。不会的   更改此设置很有用,因为使用多字节字符编码   字符和字节的数量可以完全不同。如果你   希望存储没有特定上限的长字符串,使用文本或   字符变化而没有长度说明符,而不是组成一个   任意长度限制。)

因此将其设置为4'000'000'000毫无意义。

如果您稍后进行了其他更改,这将无法解决该问题(至少不是立即解决),因为您在其中创建了带有4000000000的迁移文件。您将必须删除无法应用的迁移文件(以及这些迁移之后的文件,因为否则,中可能存在 个错误),并制作新的迁移文件。