Django不允许我将简单模型更新为sqlite DB

时间:2014-03-07 19:59:36

标签: django django-models django-database

我目前正在尝试熟悉django,我遇到了一个我无法解决的错误。我的django版本是1.7a2(来自开发人员)。

我的模型非常简单:

from django.db import models

class dataset(models.Model):
    ID = models.CharField("ID of current dataset", max_length=20, primary_key=True)
    date = models.DateTimeField('Date of current dataset')
    name = models.CharField("Name of current dataset", max_length=20)
    value = models.FloatField("Value of current dataset")

但是当我第二次运行'python manage.py syncdb'命令时,我收到此错误:

$ python manage.py syncdb
Operations to perform:
  Synchronize unmigrated apps: admin, contenttypes, auth, sessions
  Apply all migrations: display
Synchronizing apps without migrations:
  Creating tables...
  Installing custom SQL...
  Installing indexes...
Running migrations:
  Applying display.0001_initial...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-trunk/django/core/management/__init__.py", line 427, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/core/management/__init__.py", line 419, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/core/management/base.py", line 287, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/core/management/base.py", line 336, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/core/management/base.py", line 531, in handle
    return self.handle_noargs(**options)
  File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/core/management/commands/syncdb.py", line 22, in handle_noargs
    call_command("migrate", **options)
  File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/core/management/__init__.py", line 167, in call_command
    return klass.execute(*args, **defaults)
  File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/core/management/base.py", line 336, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/core/management/commands/migrate.py", line 145, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/db/migrations/executor.py", line 60, in migrate
    self.apply_migration(migration, fake=fake)
  File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/db/migrations/executor.py", line 94, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/db/migrations/migration.py", line 97, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/db/migrations/operations/models.py", line 28, in database_forwards
    schema_editor.create_model(model)
  File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/db/backends/schema.py", line 253, in create_model
    self.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/db/backends/schema.py", line 95, in execute
    cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/db/backends/utils.py", line 77, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/db/backends/utils.py", line 61, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/db/utils.py", line 93, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/db/backends/utils.py", line 61, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/db/backends/sqlite3/base.py", line 475, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: duplicate column name: ID

有谁知道我做错了什么?

提前致谢。

2 个答案:

答案 0 :(得分:1)

我尝试删除django根目录下的db.sqlite3文件并且它不起作用,但后来删除了我的模型所在的应用程序文件夹中的/ migration /的内容,并且它没有显示任何其他错误。

在我看来是某种错误,因为一旦模型改变了它就不会更新迁移文件夹......

答案 1 :(得分:1)

您无法创建名称为ID的条目。 如果您删除ID,则会显示错误。 以下是我修复它的方法。

删除迁移目录 将ID重命名为其他东西

python manage.py makemigrations python manage.py migrate

希望这会解决它。