here its my model code
class student(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
available = models.BooleanField(default=False, blank=True)
In above model available is my new field, now I want to migrate database but it give me error, when I'm run following command
python manage.py migrate
Operations to perform:
Apply all migrations: student
Running migrations:
Applying order.0004_auto_20150223_1758...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/nikhil/live-devEnv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/home/nikhil/live-devEnv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/nikhil/live-devEnv/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/nikhil/live-devEnv/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/home/nikhil/live-devEnv/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 160, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File "/home/nikhil/live-devEnv/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 63, in migrate
self.apply_migration(migration, fake=fake)
File "/home/nikhil/live-devEnv/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 97, in apply_migration
migration.apply(project_state, schema_editor)
File "/home/nikhil/live-devEnv/local/lib/python2.7/site-packages/django/db/migrations/migration.py", line 107, in apply
operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
File "/home/nikhil/live-devEnv/local/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 37, in database_forwards
field,
File "/home/nikhil/live-devEnv/local/lib/python2.7/site-packages/django/db/backends/sqlite3/schema.py", line 167, in add_field
self._remake_table(model, create_fields=[field])
File "/home/nikhil/live-devEnv/local/lib/python2.7/site-packages/django/db/backends/sqlite3/schema.py", line 135, in _remake_table
self.quote_name(model._meta.db_table),
File "/home/nikhil/live-devEnv/local/lib/python2.7/site-packages/django/db/backends/schema.py", line 99, in execute
cursor.execute(sql, params)
File "/home/nikhil/live-devEnv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/nikhil/live-devEnv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/home/nikhil/live-devEnv/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/nikhil/live-devEnv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/home/nikhil/live-devEnv/local/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 485, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: student.available
something wrong here please give me a solution for above problem.
I had this issue, when I wanted to change a field to be non-nullable, but there were already data in the database, where this value was null, which brought the conflict. I was able to resolve it by
Add the nullable field.
migrations.AddField(
model_name='mymodel',
name='new_field',
field=models.OneToOneField(to='app.OtherModel', null=True, default=None),
preserve_default=False,
),
Use RunPython to populate the fields of the existing data with something that made sense
migrations.RunPython(fix_null_fields)
Finally alter the field to be non-nullable
migrations.AlterField(
model_name='mymodel',
name='new_field',
field=models.OneToOneField(to='app.OtherModel'),
preserve_default=True,
),
This seemed to work for me without any trouble. For more information, look into Migrations and Migration operations
Related
I'm designing an Application where username will be an AutoIntegerField and unique.
Here's my model.
class ModelA(models.Model):
username = models.BigAutoField(primary_key=True, db_index=False)
user_id = models.UUIDField(default=uuid.uuid4, unique=True,
editable=False)
Initially, I wanted user_id to be a primary_key, but I can't create an AutoField which is not primary_key. As a result, I'd to let go off user_id as primary_key and assigned username as the primary key.
Now, when I run the migrations, it throws an error saying,
django.db.utils.ProgrammingError: operator class "varchar_pattern_ops" does not accept data type bigint
Complete StackTrace:-
Applying users.0005_auto_20180626_0914...Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: operator class "varchar_pattern_ops" does not accept data type bigint
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 "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/migrations/executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/Library/Frameworks/Python.framework/Versions/3.5/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 "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/migrations/operations/fields.py", line 216, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 515, in alter_field
old_db_params, new_db_params, strict)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/postgresql/schema.py", line 112, in _alter_field
new_db_params, strict,
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 684, in _alter_field
params,
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 120, in execute
cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/utils.py", line 80, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: operator class "varchar_pattern_ops" does not accept data type bigint
I think the issue is that you still have an old index on your username field that clashes with the new type. The db_index=False argument has no effect because primary_key=True always generates an index.
You might be able to solve this by removing primary_key=True, creating a migration, and then re-adding it and creating another migration. Alternatively, you can do it by hand by dropping and re-creating the index. If you want to go down that path, consult this answer.
If your project is still in an early stage and you don't have any valuable data, you can take the easy way out and drop any tables related to your users app or even the complete database, delete all migrations in the users app and create them from scratch.
in my case I was connecting django to postgres at localhost pgadmin
first deleted all the migrations except default one and also in the pycache
then just run python manage.py makemigrations and python manage.py migrate in your terminal
Sometimes during migrations in django on big tables (>50 millions rows) i receive the following error:
Traceback:
Traceback (most recent call last):
File "src/audit/manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.5/dist-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/operations/fields.py", line 86, in database_forwards
field,
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/schema.py", line 439, in add_field
self.execute(sql)
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/schema.py", line 120, in execute
cursor.execute(sql, params)
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 80, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.5/dist-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python3.5/dist-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
django.db.utils.OperationalError: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
Can you please give me advice - which postgres settings i should change in order to solve this issue?
I try set statement_timeout = 0, idle_in_transaction_session_timeout = 0 with no result.
Migration:
class Migration(migrations.Migration):
dependencies = [
('core', '0028_auto_'),
]
operations = [
migrations.AddField(
model_name='risk',
name='test3',
field=models.CharField(default='testme', max_length=50),
),
]
SQL:
BEGIN;
--
-- Add field test3 to risk
--
ALTER TABLE "core_risk" ADD COLUMN "test3" varchar(50) DEFAULT 'testme' NOT NULL;
ALTER TABLE "core_risk" ALTER COLUMN "test3" DROP DEFAULT;
COMMIT;
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 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)
I have been reading tango_with_django tutorial, and when I managed to get to part 7, I was stucked.
When I added this code
from django.template.defaultfilters import slugify
class Category(models.Model):
name = models.CharField(max_length=128, unique=True)
views = models.IntegerField(default=0)
likes = models.IntegerField(default=0)
slug = models.SlugField(unique=True)
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(Category, self).save(*args, **kwargs)
def __unicode__(self):
return self.name
I've got this
Traceback (most recent call last): File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv) File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 385, in execute_from_command_line
utility.execute() File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\Python34\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__) File "C:\Python34\lib\site-packages\django\core\management\base.py", line 338, in execute
output = self.handle(*args, **options) File "C:\Python34\lib\site-packages\django\core\management\commands\migrate.py ", line 161, in handle
executor.migrate(targets, plan, fake=options.get("fake", False)) File "C:\Python34\lib\site-packages\django\db\migrations\executor.py", line 68 , in migrate
self.apply_migration(migration, fake=fake) File "C:\Python34\lib\site-packages\django\db\migrations\executor.py", line 10 2, in apply_migration
migration.apply(project_state, schema_editor) File "C:\Python34\lib\site-packages\django\db\migrations\migration.py", line 1 08, in apply
operation.database_forwards(self.app_label, schema_editor, project_state, ne w_state) File "C:\Python34\lib\site-packages\django\db\migrations\operations\fields.py" , line 37, in database_forwards
field, File "C:\Python34\lib\site-packages\django\db\backends\sqlite3\schema.py", lin e 176, in add_field
self._remake_table(model, create_fields=[field]) File "C:\Python34\lib\site-packages\django\db\backends\sqlite3\schema.py", lin e 144, in _remake_table
self.quote_name(model._meta.db_table), File "C:\Python34\lib\site-packages\django\db\backends\schema.py", line 102, i n execute
cursor.execute(sql, params) File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 81, in execute
return super(CursorDebugWrapper, self).execute(sql, params) File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 65, in execute
return self.cursor.execute(sql, params) File "C:\Python34\lib\site-packages\django\db\utils.py", line 94, in
__exit__
six.reraise(dj_exc_type, dj_exc_value, traceback) File "C:\Python34\lib\site-packages\django\utils\six.py", line 658, in reraise
raise value.with_traceback(tb) File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 65, in execute
return self.cursor.execute(sql, params) File "C:\Python34\lib\site-packages\django\db\backends\sqlite3\base.py", line 485, in execute
return Database.Cursor.execute(self, query, params) django.db.utils.IntegrityError: UNIQUE constraint failed: rango_category__new.sl ug
After I removed this code, I still get same error message while trying to do
python manage.py migrate
After I deleted database with
python manage.py flush
And entered
python manage.py migrate
I also got the same error.
What's going on?
try rolling back the migration to the previous state. Assuming this is your first migration - lets roll back to zero
python manage.py migrate category zero
slug logic can get a bit complex
despite names being different they can have the same slug
e.g.
Name: Foo Bar
Slug: foo-bar
Name: foo bar
Slug: foo-bar
Name: Foo Bar
Slug: foo-bar
Name: Foo! Bar
Slug: foo-bar
...you get the idea
you defiantly want unique slugs to make url access nice
e.g. domain.com/category/1234 (using id)
vs domain.com/category/foo-bar
have a look at AutoSlugField which will do lots of the hard work for you in terms of making unique slugs