【问题标题】:django wrong in the migrationdjango 在迁移中出错
【发布时间】:2019-05-17 01:28:15
【问题描述】:

怎么了?

C:\holamundo>python manage.py makemigrations App
Migrations for 'App':
  App\migrations\0002_auto_20181216_0745.py
    - Alter field nombre on tabla

resultado:

C:\holamundo>python manage.py migrate
Operations to perform:
  Apply all migrations: App, admin, auth, contenttypes, sessions
Running migrations:
  Applying App.0001_initial...Traceback (most recent call last):
  File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\backends\utils.py", line 83, in _execute
    return self.cursor.execute(sql)
  File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\backends\sqlite3\base.py", line 294, in execute
    return Database.Cursor.execute(self, query)
sqlite3.OperationalError: duplicate column name: ID

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\core\management\base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\core\management\base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\core\management\base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\core\management\commands\migrate.py", line 203, in handle
    fake_initial=fake_initial,
  File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\migrations\executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\migrations\executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\migrations\migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\migrations\operations\models.py", line 91, in database_forwards
    schema_editor.create_model(model)
  File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\backends\base\schema.py", line 312, in create_model
    self.execute(sql, params or None)
  File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\backends\base\schema.py", line 133, in execute
    cursor.execute(sql, params)
  File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\backends\utils.py", line 100, in execute
    return super().execute(sql, params)
  File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\backends\utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\backends\utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\backends\utils.py", line 83, in _execute
    return self.cursor.execute(sql)
  File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\backends\sqlite3\base.py", line 294, in execute
    return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: duplicate column name: ID

桌子:

from django.db import models

# Create your models here.
class tabla (models.Model):
    ID = models.PositiveSmallIntegerField()
    nombre = models.CharField(max_length=50)
    edad = models.PositiveSmallIntegerField()
    comida = models.PositiveSmallIntegerField()
    cantidad = models.PositiveSmallIntegerField()

    def datos(self):
        cadena="{0}, {1} {2} {3} {4}"
        return cadena.format(self.ID, self.nombre,self.edad,self.comida,self.cantidad)

    def __str__(self):
        return self.datos()

【问题讨论】:

  • 为什么要显式设置一个 ID 字段,而不是把它作为主键?您应该删除该字段并使用自动生成的字段。

标签: django django-models model


【解决方案1】:

如果要覆盖 id 字段,则需要添加 primary_key=True。

id = models.PositiveIntegerField(primary_key=True)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-28
    • 2020-05-03
    • 1970-01-01
    • 1970-01-01
    • 2019-09-22
    • 2019-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多