2016-08-23 5 views
0

에 실패했습니다. 왜 고객 ID에 unique=True을 추가 한 후에도 마이그레이션을 실행 한 후에도 여전히이 오류가 발생합니까? 당신이 already having some objects as per old model definitions을처럼장고는 UNIQUE 제약이 내 models.py는 모습입니다 오류

Error: 
     Rendering model states... DONE 
     Applying newuser.0003_auto_20160823_0128...Traceback (most recent call last): 
      File "/home/krag91/Documents/djangodev/virtualenv /lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in  execute 
    return self.cursor.execute(sql, params) 
      File "/home/krag91/Documents/djangodev/virtualenv /lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 337, in execute 
    return Database.Cursor.execute(self, query, params) 
     sqlite3.IntegrityError: UNIQUE constraint failed:  newuser_customer.customer_id 
The above exception was the direct cause of the following exception: 

Traceback (most recent call last): 
    File "manage.py", line 22, in <module> 
    execute_from_command_line(sys.argv) 
    File "/home/krag91/Documents/djangodev/virtualenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line utility.execute() 
    File "/home/krag91/Documents/djangodev/virtualenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 359, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "/home/krag91/Documents/djangodev/virtualenv/lib/python3.5/site-packages/django/core/management/base.py", line 305, in run_from_argv 
    self.execute(*args, **cmd_options) 
    File "/home/krag91/Documents/djangodev/virtualenv/lib/python3.5/site-packages/django/core/management/base.py", line 356, in execute 
    output = self.handle(*args, **options) 
    File "/home/krag91/Documents/djangodev/virtualenv/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 202, in handle 
    targets, plan, fake=fake, fake_initial=fake_initial 
    File "/home/krag91/Documents/djangodev/virtualenv/lib/python3.5/site-packages/django/db/migrations/executor.py", line 97, in migrate 
    state = self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial) 
    File "/home/krag91/Documents/djangodev/virtualenv/lib/python3.5/site-packages/django/db/migrations/executor.py", line 132, in _migrate_all_forwards 
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial) 
    File "/home/krag91/Documents/djangodev/virtualenv/lib/python3.5/site-packages/django/db/migrations/executor.py", line 237, in apply_migration 
    state = migration.apply(state, schema_editor) 
    File "/home/krag91/Documents/djangodev/virtualenv/lib/python3.5/site-packages/django/db/migrations/migration.py", line 129, in apply 
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state) 
    File "/home/krag91/Documents/djangodev/virtualenv/lib/python3.5/site-packages/django/db/migrations/operations/fields.py", line 84, in database_forwards 
    field, 
    File "/home/krag91/Documents/djangodev/virtualenv/lib/python3.5/site-packages/django/db/backends/sqlite3/schema.py", line 231, in add_field 
    self._remake_table(model, create_fields=[field]) 
    File "/home/krag91/Documents/djangodev/virtualenv/lib/python3.5/site-packages/django/db/backends/sqlite3/schema.py", line 199, in _remake_table 
    self.quote_name(model._meta.db_table), 
    File "/home/krag91/Documents/djangodev/virtualenv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 112, in execute 
    cursor.execute(sql, params) 
    File "/home/krag91/Documents/djangodev/virtualenv/lib/python3.5/site-packages/django/db/backends/utils.py", line 79, in execute 
    return super(CursorDebugWrapper, self).execute(sql, params) 
    File "/home/krag91/Documents/djangodev/virtualenv/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute 
    return self.cursor.execute(sql, params) 
    File "/home/krag91/Documents/djangodev/virtualenv/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__ 
    six.reraise(dj_exc_type, dj_exc_value, traceback) 
    File "/home/krag91/Documents/djangodev/virtualenv/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise 
    raise value.with_traceback(tb) 
    File "/home/krag91/Documents/djangodev/virtualenv/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute 
    return self.cursor.execute(sql, params) 
    File "/home/krag91/Documents/djangodev/virtualenv/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 337, in execute 
    return Database.Cursor.execute(self, query, params) 
django.db.utils.IntegrityError: UNIQUE constraint failed: newuser_customer.customer_id 

답변

0

보인다. By default django creates a field named as id to every model in database. modelName.id를 통해 액세스 할 수 있습니다.

당신의 경우 나는 당신이 기본 키로 기본으로 customer.id 데이터베이스에서 일부 개체를 가지고 있다는 것입니다. 따라서 모델을 변경하고 마이그레이션을 적용한 경우 existing objects are checked and another unique field is tried to added as a primary key. 여기서 해결 방법은 customer_id 필드를 제거한 후 기존 개체를 모두 삭제 한 다음 필드를 다시 만들고 마이그레이션을 실행하는 것입니다.

HTH