2016-07-23 97 views
0

我有一個笑話型號:Django的:重複鍵值違反唯一約束

class Joke(models.Model): 
... 
    date_created = models.DateTimeField(default=datetime.now, blank=True) 
    date_modified = models.DateTimeField(default=datetime.now, blank=True) 
    creator = models.OneToOneField(User, default=1) 

現在,當我嘗試遷移的最後一行我得到的錯誤。基本上,我希望將用戶鏈接到笑話對象,因爲我已經有一個數據庫,我想要的默認值是1,這是管理員用戶的ID(我查了...)。 makemigrations工作得很好,但當我嘗試遷移時,我得到這個:

Operations to perform: 
    Apply all migrations: jokes_app, sessions, contenttypes, auth, taggit, default, admin 
Running migrations: 
    Rendering model states... DONE 
    Applying jokes_app.0008_auto_20160723_1559...Traceback (most recent call last): 
    File "/home/antoni4040/Documents/Jokes_Website/django-jokes/venv/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute 
    return self.cursor.execute(sql, params) 
psycopg2.IntegrityError: duplicate key value violates unique constraint "jokes_app_joke_creator_id_key" 
DETAIL: Key (creator_id)=(1) already exists. 


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

Traceback (most recent call last): 
    File "./manage.py", line 10, in <module> 
    execute_from_command_line(sys.argv) 
    File "/home/antoni4040/Documents/Jokes_Website/django-jokes/venv/lib/python3.4/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line 
    utility.execute() 
    File "/home/antoni4040/Documents/Jokes_Website/django-jokes/venv/lib/python3.4/site-packages/django/core/management/__init__.py", line 345, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "/home/antoni4040/Documents/Jokes_Website/django-jokes/venv/lib/python3.4/site-packages/django/core/management/base.py", line 348, in run_from_argv 
    self.execute(*args, **cmd_options) 
    File "/home/antoni4040/Documents/Jokes_Website/django-jokes/venv/lib/python3.4/site-packages/django/core/management/base.py", line 399, in execute 
    output = self.handle(*args, **options) 
    File "/home/antoni4040/Documents/Jokes_Website/django-jokes/venv/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 200, in handle 
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial) 
    File "/home/antoni4040/Documents/Jokes_Website/django-jokes/venv/lib/python3.4/site-packages/django/db/migrations/executor.py", line 92, in migrate 
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial) 
    File "/home/antoni4040/Documents/Jokes_Website/django-jokes/venv/lib/python3.4/site-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 "/home/antoni4040/Documents/Jokes_Website/django-jokes/venv/lib/python3.4/site-packages/django/db/migrations/executor.py", line 198, in apply_migration 
    state = migration.apply(state, schema_editor) 
    File "/home/antoni4040/Documents/Jokes_Website/django-jokes/venv/lib/python3.4/site-packages/django/db/migrations/migration.py", line 123, in apply 
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state) 
    File "/home/antoni4040/Documents/Jokes_Website/django-jokes/venv/lib/python3.4/site-packages/django/db/migrations/operations/fields.py", line 201, in database_forwards 
    schema_editor.alter_field(from_model, from_field, to_field) 
    File "/home/antoni4040/Documents/Jokes_Website/django-jokes/venv/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 482, in alter_field 
    old_db_params, new_db_params, strict) 
    File "/home/antoni4040/Documents/Jokes_Website/django-jokes/venv/lib/python3.4/site-packages/django/db/backends/postgresql/schema.py", line 110, in _alter_field 
    new_db_params, strict, 
    File "/home/antoni4040/Documents/Jokes_Website/django-jokes/venv/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 644, in _alter_field 
    [new_default], 
    File "/home/antoni4040/Documents/Jokes_Website/django-jokes/venv/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 110, in execute 
    cursor.execute(sql, params) 
    File "/home/antoni4040/Documents/Jokes_Website/django-jokes/venv/lib/python3.4/site-packages/django/db/backends/utils.py", line 79, in execute 
    return super(CursorDebugWrapper, self).execute(sql, params) 
    File "/home/antoni4040/Documents/Jokes_Website/django-jokes/venv/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute 
    return self.cursor.execute(sql, params) 
    File "/home/antoni4040/Documents/Jokes_Website/django-jokes/venv/lib/python3.4/site-packages/django/db/utils.py", line 95, in __exit__ 
    six.reraise(dj_exc_type, dj_exc_value, traceback) 
    File "/home/antoni4040/Documents/Jokes_Website/django-jokes/venv/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise 
    raise value.with_traceback(tb) 
    File "/home/antoni4040/Documents/Jokes_Website/django-jokes/venv/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute 
    return self.cursor.execute(sql, params) 
django.db.utils.IntegrityError: duplicate key value violates unique constraint "jokes_app_joke_creator_id_key" 
DETAIL: Key (creator_id)=(1) already exists. 

我真的不明白什麼是錯的。有任何想法嗎?

回答

3

OneToOne領域強制執行,因爲它的名字一樣,一個一比一的關係,而你的情況是指一個用戶可以是一個創造者,只有一個笑話 - 絕對不是你想要的。改爲使用ForeignKey

creator = models.ForeignKey(User, default=1, on_delete=models.SET_DEFAULT) 
+1

我絕對是一個白癡。謝謝,我正準備把顯示器扔出窗外...... – Antoni4040

相關問題