Initially my model looked something like this:
from __future__ import unicode_literals
from django.template.defaultfilters import slugify
from django.contrib.auth.models import User
from django.db import models
from datetime import datetime
class blogpost(models.Model):
title = models.CharField(max_length = 200)
body = models.TextField()
createdon = models.DateTimeField(auto_now_add=True)
slug = models.SlugField(unique=True)
author = models.ForeignKey(User)
I applied makemigrations and then migrate to this model. Now, I wanted to remove the field slug from this model. I removed it and then applied makemigrations.
^C(mysite)shivam#shivam-HP-Pavilion-15-Notebook-PC:~/Python/django/mysite/mysite$ python manage.py makemigrations
Migrations for 'blog':
0003_remove_blogpost_slug.py:
- Remove field slug from blogpost
But now when I apply migrate, it gives me an error.
Operations to perform:
Apply all migrations: admin, blog, contenttypes, auth, sessions
Running migrations:
Rendering model states... DONE
Applying blog.0003_remove_blogpost_slug...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/shivam/Python/django/mysite/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
utility.execute()
File "/home/shivam/Python/django/mysite/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 342, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/shivam/Python/django/mysite/local/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/shivam/Python/django/mysite/local/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/home/shivam/Python/django/mysite/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 200, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/home/shivam/Python/django/mysite/local/lib/python2.7/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/shivam/Python/django/mysite/local/lib/python2.7/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/shivam/Python/django/mysite/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/shivam/Python/django/mysite/local/lib/python2.7/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/shivam/Python/django/mysite/local/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 121, in database_forwards
schema_editor.remove_field(from_model, from_model._meta.get_field(self.name))
File "/home/shivam/Python/django/mysite/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 438, in remove_field
self.execute(sql)
File "/home/shivam/Python/django/mysite/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 110, in execute
cursor.execute(sql, params)
File "/home/shivam/Python/django/mysite/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/shivam/Python/django/mysite/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/shivam/Python/django/mysite/local/lib/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/shivam/Python/django/mysite/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/shivam/Python/django/mysite/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 112, in execute
return self.cursor.execute(query, args)
File "/home/shivam/Python/django/mysite/local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "/home/shivam/Python/django/mysite/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
django.db.utils.OperationalError: (1091, "Can't DROP 'slug'; check that column/key exists")
What should I do to remove the slug field?
Related
I created an app two days ago, added some models, and migrated successfully. And since, I haven't added or changed anything but I added something today and tried to migrate, after making migrations and tried to migrate, I got this error:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, students, user_upload
Running migrations:
Applying user_upload.0002_users_upload_phone_number...Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\core\management\base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\core\management\base.py", line 371, in execute
output = self.handle(*args, **options)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\core\management\base.py", line 85, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\core\management\commands\migrate.py", line 243, in handle
post_migrate_state = executor.migrate(
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\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\aser\AppData\Roaming\Python\Python38\site-packages\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\aser\AppData\Roaming\Python\Python38\site-packages\django\db\migrations\executor.py", line 227, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\db\migrations\migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\db\migrations\operations\fields.py", line 104, in database_forwards
schema_editor.add_field(
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\db\backends\sqlite3\schema.py", line 328, in add_field
self._remake_table(model, create_field=field)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\db\backends\sqlite3\schema.py", line 189, in _remake_table
self.effective_default(create_field)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\db\backends\base\schema.py", line 303, in effective_default
return field.get_db_prep_save(self._effective_default(field), self.connection)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\db\models\fields\__init__.py", line 1512, in get_db_prep_save
return connection.ops.adapt_decimalfield_value(self.to_python(value), self.max_digits, self.decimal_places)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\db\backends\base\operations.py", line 523, in adapt_decimalfield_value
return utils.format_number(value, max_digits, decimal_places)
File "C:\Users\aser\AppData\Roaming\Python\Python38\site-packages\django\db\backends\utils.py", line 235, in format_number
value = value.quantize(decimal.Decimal(1).scaleb(-decimal_places), context=context)
decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]
I've been looking but can't really find what's wrong
This is my model
from django.db import models
from django.forms import ModelForm
class Users_Upload(models.Model):
first_name = models.CharField(max_length=120)
last_name = models.CharField(max_length=120)
email = models.CharField(max_length=120)
phone_number = models.DecimalField(max_digits=14, decimal_places=4)
address = models.CharField(max_length=120, default="Bay Area, San Francisco, California, USA")
and my admin
from django.contrib import admin
from .models import Users_Upload
admin.site.register(Users_Upload)
I've already solved it. I had to clear out all my migrations and re-migrate again.
disclaimer : I've gone through the django - documentation, but didn't come to a conclusion.
I was handed over a project of my senior in college to work on with.
In an application in the project: accounts - obviously it's to create user accounts.
in models.py
from django.contrib import auth
from django.db import models
class User(auth.models.User, auth.models.PermissionsMixin):
def __str__(self):
return "#{}".format(self.username)
though in the forms.py this model was not used. forms.py
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm
from django import forms
class UserCreateForm(UserCreationForm):
class Meta:
fields = ("first_name", "last_name", "username", "email", "password1", "password2")
model = get_user_model()
But here the imported model is something other than the defined model in models.py, rather he imported the model from from django.contrib.auth
So I've tried to comment out the model from the models.py and migrate, it yields an error.
django.db.utils.OperationalError: near ")": syntax error
rather instead of model = get_user_model I've used
model = User (from models.py)
even this yield's error unable to import User
Could anyone please explain me what's the relation and why should I use both the models?? Since the senior is no more, I've no chance of contacting him.
Tracebacks -
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/home/user/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/user/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/user/.local/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/user/.local/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
output = self.handle(*args, **options)
File "/home/user/.local/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/home/user/.local/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 203, in handle
fake_initial=fake_initial,
File "/home/user/.local/lib/python3.6/site-packages/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 "/home/user/.local/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/home/user/.local/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/user/.local/lib/python3.6/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/user/.local/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 150, in database_forwards
schema_editor.remove_field(from_model, from_model._meta.get_field(self.name))
File "/home/user/.local/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py", line 318, in remove_field
self._remake_table(model, delete_field=field)
File "/home/user/.local/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py", line 265, in _remake_table
self.quote_name(model._meta.db_table),
File "/home/user/.local/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 133, in execute
cursor.execute(sql, params)
File "/home/user/.local/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/home/user/.local/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/user/.local/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/user/.local/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/home/user/.local/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/user/.local/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/home/user/.local/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 296, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: near ")": syntax error
my app named mainsite
I have the table class named Weather in models.py
class Weather(models.Model):
tpr = models.CharField(max_length=5)
wet = models.CharField(max_length=5)
ur = models.CharField(max_length=5)
li = models.CharField(max_length=5)
observe_time = models.DateTimeField(default=None)
I don't want my observe_time set any deafult value
So I set to None .
I have made migrations.
when I made migrate
Then the traceback happen:
File "manage.py", line 22, in
execute_from_command_line(sys.argv)
File "C:\Users\User\Anaconda3\lib\site-packages\django\core\management__init__.py",
line 354, in execute_from_command_line
utility.execute() File "C:\Users\User\Anaconda3\lib\site-packages\django\core\management\__init__.py",
line 346, in execute
self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\Users\User\Anaconda3\lib\site-packages\django\core\management\base.py",
line 394, in run_from_argv
self.execute(*args, **cmd_options) File "C:\Users\User\Anaconda3\lib\site-packages\django\core\management\base.py",
line 445, in execute
output = self.handle(*args, **options) File "C:\Users\User\Anaconda3\lib\site-packages\django\core\management\commands\migrate.py",
line 222, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial) File
"C:\Users\User\Anaconda3\lib\site-packages\django\db\migrations\executor.py",
line 110, in migrate
self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial) File
"C:\Users\User\Anaconda3\lib\site-packages\django\db\migrations\executor.py",
line 148, in apply_migration
state = migration.apply(state, schema_editor) File "C:\Users\User\Anaconda3\lib\site-packages\django\db\migrations\migration.py",
line 115, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state) File
"C:\Users\User\Anaconda3\lib\site-packages\django\db\migrations\operations\fields.py",
line 62, in database_forwards
field, File "C:\Users\User\Anaconda3\lib\site-packages\django\db\backends\sqlite3\schema.py",
line 179, in add_field
self._remake_table(model, create_fields=[field]) File "C:\Users\User\Anaconda3\lib\site-packages\django\db\backends\sqlite3\schema.py",
line 147, in _remake_table
self.quote_name(model._meta.db_table), File "C:\Users\User\Anaconda3\lib\site-packages\django\db\backends\base\schema.py",
line 111, in execute
cursor.execute(sql, params) File "C:\Users\User\Anaconda3\lib\site-packages\django\db\backends\utils.py",
line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params) File "C:\Users\User\Anaconda3\lib\site-packages\django\db\backends\utils.py",
line 64, in execute
return self.cursor.execute(sql, params) File "C:\Users\User\Anaconda3\lib\site-packages\django\db\utils.py", line
98, in exit
six.reraise(dj_exc_type, dj_exc_value, traceback) File "C:\Users\User\Anaconda3\lib\site-packages\django\utils\six.py", line
685, in reraise
raise value.with_traceback(tb) File "C:\Users\User\Anaconda3\lib\site-packages\django\db\backends\utils.py",
line 64, in execute
return self.cursor.execute(sql, params) File "C:\Users\User\Anaconda3\lib\site-packages\django\db\backends\sqlite3\base.py",
line 318, in execute
return Database.Cursor.execute(self, query, params) django.db.utils.IntegrityError: NOT NULL constraint failed:
mainsite_weather__new.observe_time
I don't know what caused this error.
thx.
In order for this to work you should also make this field nullable and blankable, like this:
observe_time = models.DateTimeField(default=None, blank=True, null=True)
or omit default=None completely, like this:
observe_time = models.DateTimeField(blank=True, null=True)
Also, after that, don't forget to delete the previous migration file and run makemigrations and migrate again.
I am using PostgreSQL as my database and using django tenant schemas. But when I run the following command python manage.py migrate_schemas --shared I get the error saying
(checkpoint_env)
G:\Django_Projects\RackNole\checkpoint_shashi_2\checkpoint_shashi_2>python manage.py migrate_schemas --shared
System check identified some issues:
WARNINGS:
checkpoint.MultiSelectResponse.response: (fields.W340) null has no effect on ManyToManyField.
=== Running migrate for schema public
System check identified some issues:
WARNINGS:
checkpoint.MultiSelectResponse.response: (fields.W340) null has no effect on ManyToManyField.
Operations to perform:
Apply all migrations: website, checkpoint, account, django_comments, redirects, core, admin, twitter, galleries, tastypie, customers, auth, sites, blog, generic, contenttypes, sessions, conf, forms, pages, socialaccount
Running migrations:
Rendering model states... DONE
Applying core.0002_auto_20150414_2140...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "G:\Py_Envs\Racknole\checkpoint_env\lib\site-packages\django\core\management\__init__.py", line 350, in execute_from_command_line
utility.execute()
File "G:\Py_Envs\Racknole\checkpoint_env\lib\site-packages\django\core\management\__init__.py", line 342, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "G:\Py_Envs\Racknole\checkpoint_env\lib\site-packages\django\core\management\base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "G:\Py_Envs\Racknole\checkpoint_env\lib\site-packages\django\core\management\base.py", line 399, in execute
output = self.handle(*args, **options)
File "G:\Py_Envs\Racknole\checkpoint_env\lib\site-packages\tenant_schemas\management\commands\migrate_schemas.py", line 42, in handle
self.run_migrations(self.schema_name, settings.SHARED_APPS)
File "G:\Py_Envs\Racknole\checkpoint_env\lib\site-packages\tenant_schemas\management\commands\migrate_schemas.py", line 65, in run_migrations
command.execute(*self.args, **self.options)
File "G:\Py_Envs\Racknole\checkpoint_env\lib\site-packages\django\core\management\base.py", line 399, in execute
output = self.handle(*args, **options)
File "G:\Py_Envs\Racknole\checkpoint_env\lib\site-packages\django\core\management\commands\migrate.py", line 200, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "G:\Py_Envs\Racknole\checkpoint_env\lib\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 "G:\Py_Envs\Racknole\checkpoint_env\lib\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 "G:\Py_Envs\Racknole\checkpoint_env\lib\site-packages\django\db\migrations\executor.py", line 198, in apply_migration
state = migration.apply(state, schema_editor)
File "G:\Py_Envs\Racknole\checkpoint_env\lib\site-packages\django\db\migrations\migration.py", line 123, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "G:\Py_Envs\Racknole\checkpoint_env\lib\site-packages\django\db\migrations\operations\fields.py", line 201, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "G:\Py_Envs\Racknole\checkpoint_env\lib\site-packages\django\db\backends\base\schema.py", line 482, in alter_field
old_db_params, new_db_params, strict)
File "G:\Py_Envs\Racknole\checkpoint_env\lib\site-packages\django\db\backends\base\schema.py", line 491, in _alter_field
fk_names = self._constraint_names(model, [old_field.column], foreign_key=True)
File "G:\Py_Envs\Racknole\checkpoint_env\lib\site-packages\django\db\backends\base\schema.py", line 911, in _constraint_names
constraints = self.connection.introspection.get_constraints(cursor, model._meta.db_table)
File "G:\Py_Envs\Racknole\checkpoint_env\lib\site-packages\tenant_schemas\postgresql_backend\introspection.py", line 287, in get_constraints
'table': table_name,
File "G:\Py_Envs\Racknole\checkpoint_env\lib\site-packages\django\db\backends\utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "G:\Py_Envs\Racknole\checkpoint_env\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "G:\Py_Envs\Racknole\checkpoint_env\lib\site-packages\django\db\utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "G:\Py_Envs\Racknole\checkpoint_env\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column am.amcanorder does not exist
LINE 13: WHEN am.amcanorder THEN
In PostgreSQL up to 9.5 amcanorder was pg_am catalog column. In PostgreSQL 9.6 it was removed. Apparently, django-tenant-schemas uses it.
I have a Joke model:
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)
Now, when I try to migrate the last line I get errors. Basically, I want to link a User to the Joke object, and since I already have a database, I want the default value to be 1, which is the admin user's id(I checked...). Makemigrations works just fine but when I try to migrate, I get this:
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.
I really don't understand what's wrong. Any ideas?
OneToOne field enforces, as it's name says, an one-to-one relationship, which in your case means that one user can be creator of one and only one joke - definitely not what you want. Use ForeignKey instead:
creator = models.ForeignKey(User, default=1, on_delete=models.SET_DEFAULT)