So I am using south for my django project, and have been changing some model logic. I changed a bunch of things, but regardless, my model looks like this now:
class Group(models.Model):
'''table of organizational groups'''
group_id = models.AutoField(primary_key=True)
group_name = models.CharField(max_length=100,unique=True) #name of group as string
icon_path_clicked = models.CharField(blank=True,max_length=255) #location icon is stored
icon_path_unclicked = models.CharField(blank=True,max_length=255) #location icon is stored
group_description = models.CharField(max_length=255) #description of the group
is_active = models.BooleanField(default=True)
date_created = models.DateTimeField(auto_now_add=True)
now previously, both icon_path_clicked and icon_path_unclicked had unique=True, but I took that out, yet when I try and migrate, I get the following error:
django.db.utils.IntegrityError: could not create unique index "everything_group_icon_path_clicked_key"
DETAIL: Key (icon_path_clicked)=() is duplicated.
why is it still treating this as if I no longer have that field as unique?
full traceback
Running migrations for app:
- Migrating forwards to 0013_auto__chg_field_group_icon_path_clicked__chg_field_group_icon_path_unc.
> everything:0007_auto__add_field_group_icon_path_clicked__add_field_group_icon_path_unc
FATAL ERROR - The following SQL query failed: ALTER TABLE "everything_group" ADD COLUMN "icon_path_clicked" varchar(255) NOT NULL UNIQUE DEFAULT '';
The error was: could not create unique index "everything_group_icon_path_clicked_key"
DETAIL: Key (icon_path_clicked)=() is duplicated.
Error in migration: everything:0007_auto__add_field_group_icon_path_clicked__add_field_group_icon_path_unc
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Python/2.7/site-packages/Django-1.6-py2.7.egg/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/Library/Python/2.7/site-packages/Django-1.6-py2.7.egg/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Python/2.7/site-packages/Django-1.6-py2.7.egg/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Library/Python/2.7/site-packages/Django-1.6-py2.7.egg/django/core/management/base.py", line 285, in execute
output = self.handle(*args, **options)
File "/Library/Python/2.7/site-packages/South-0.8.3-py2.7.egg/south/management/commands/migrate.py", line 111, in handle
ignore_ghosts = ignore_ghosts,
File "/Library/Python/2.7/site-packages/South-0.8.3-py2.7.egg/south/migration/__init__.py", line 220, in migrate_app
success = migrator.migrate_many(target, workplan, database)
File "/Library/Python/2.7/site-packages/South-0.8.3-py2.7.egg/south/migration/migrators.py", line 230, in migrate_many
result = migrator.__class__.migrate_many(migrator, target, migrations, database)
File "/Library/Python/2.7/site-packages/South-0.8.3-py2.7.egg/south/migration/migrators.py", line 305, in migrate_many
result = self.migrate(migration, database)
File "/Library/Python/2.7/site-packages/South-0.8.3-py2.7.egg/south/migration/migrators.py", line 134, in migrate
result = self.run(migration, database)
File "/Library/Python/2.7/site-packages/South-0.8.3-py2.7.egg/south/migration/migrators.py", line 115, in run
return self.run_migration(migration, database)
File "/Library/Python/2.7/site-packages/South-0.8.3-py2.7.egg/south/migration/migrators.py", line 85, in run_migration
migration_function()
File "/Library/Python/2.7/site-packages/South-0.8.3-py2.7.egg/south/migration/migrators.py", line 61, in <lambda>
return (lambda: direction(orm))
File "/Users/ryansaxe/Documents/project/app/migrations/0007_auto__add_field_group_icon_path_clicked__add_field_group_icon_path_unc.py", line 14, in forwards
keep_default=False)
File "/Library/Python/2.7/site-packages/South-0.8.3-py2.7.egg/south/db/generic.py", line 47, in _cache_clear
return func(self, table, *args, **opts)
File "/Library/Python/2.7/site-packages/South-0.8.3-py2.7.egg/south/db/generic.py", line 418, in add_column
self.execute(sql)
File "/Library/Python/2.7/site-packages/South-0.8.3-py2.7.egg/south/db/generic.py", line 282, in execute
cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/Django-1.6-py2.7.egg/django/db/backends/util.py", line 69, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Library/Python/2.7/site-packages/Django-1.6-py2.7.egg/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/Django-1.6-py2.7.egg/django/db/utils.py", line 99, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Library/Python/2.7/site-packages/Django-1.6-py2.7.egg/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: could not create unique index "everything_group_icon_path_clicked_key"
DETAIL: Key (icon_path_clicked)=() is duplicated.
Related
I am very new to django(version 1.11).I am trying to make a shoppingwebsite and i am confused in creating Order model.
In here you can see my first (version 1) models.py
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.contrib.postgres.fields import ArrayField
class Products(models.Model):
name = models.CharField(max_length = 20 , default='')
description = models.CharField(max_length = 100 , default='')
price = models.IntegerField(default=0)
image = models.ImageField(upload_to='product_image',blank=True)
vojood = models.BooleanField(default=False)
unique = models.CharField(max_length = 100 ,default='')
typ = models.CharField(max_length = 100 ,default='')
def __str__ (self):
return self.name
#classmethod
def turn_on(prds,pk):
prds[pk].vojood=true
#classmethod
def turn_off(prds,pk):
prds[pk].vojood=false
class Order(models.Model):
username = models.CharField(max_length = 300 , default='')
address = models.CharField(max_length = 300 , default='')
time = models.IntegerField(default=0)
price = models.IntegerField(default=0)
arrived = models.BooleanField(default=False)
basket = ArrayField(models.CharField(max_length=300,default=''),default=list)
def __str__ (self):
return self.username
After python manage.py makemigrations when i tried to migrate the new field (arrayfield) with python manage.py migrate i got a very long error . So the last line was django.db.utils.OperationalError: near "[]": syntax errorSo i deleted the new field(arrayfield) and again i used makemigrations but the error during migrate did not change.And now i cant migrate any field with any type!
And here is my Error :
E:\django projects\4\third>python manage.py makemigrations
Migrations for 'shop':
shop\migrations\0013_auto_20180611_1518.py
- Alter field basket on order
E:\django projects\4\third>python manage.py migrate
Operations to perform:
Apply all migrations: accounts, admin, auth, contenttypes, sessions, shop
Running migrations:
Applying shop.0005_order_prds...Traceback (most recent call last):
File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 63, in
execute
return self.cursor.execute(sql)
File "C:\Python34\lib\site-packages\django\db\backends\sqlite3\base.py", line
326, in execute
return Database.Cursor.execute(self, query)
sqlite3.OperationalError: near "[]": syntax error
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 "C:\Python34\lib\site-packages\django\core\management\__init__.py", line
364, in execute_from_command_line
utility.execute()
File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line
356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python34\lib\site-packages\django\core\management\base.py", line 283,
in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Python34\lib\site-packages\django\core\management\base.py", line 330,
in execute
output = self.handle(*args, **options)
File "C:\Python34\lib\site-packages\django\core\management\commands\migrate.py
", line 204, in handle
fake_initial=fake_initial,
File "C:\Python34\lib\site-packages\django\db\migrations\executor.py", line 11
5, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_i
nitial=fake_initial)
File "C:\Python34\lib\site-packages\django\db\migrations\executor.py", line 14
5, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_
initial)
File "C:\Python34\lib\site-packages\django\db\migrations\executor.py", line 24
4, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Python34\lib\site-packages\django\db\migrations\migration.py", line 1
29, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, projec
t_state)
File "C:\Python34\lib\site-packages\django\db\migrations\operations\fields.py"
, line 87, in database_forwards
field,
File "C:\Python34\lib\site-packages\django\db\backends\sqlite3\schema.py", lin
e 238, in add_field
self._remake_table(model, create_field=field)
File "C:\Python34\lib\site-packages\django\db\backends\sqlite3\schema.py", lin
e 198, in _remake_table
self.create_model(temp_model)
File "C:\Python34\lib\site-packages\django\db\backends\base\schema.py", line 3
03, in create_model
self.execute(sql, params or None)
File "C:\Python34\lib\site-packages\django\db\backends\base\schema.py", line 1
20, in execute
cursor.execute(sql, params)
File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 80, 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 685, in reraise
raise value.with_traceback(tb)
File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 63, in
execute
return self.cursor.execute(sql)
File "C:\Python34\lib\site-packages\django\db\backends\sqlite3\base.py", line
326, in execute
return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: near "[]": syntax error
E:\django projects\4\third>
I also changed the name of array field from prds to basket but you can see in the error the name did not change !!!!! And it runs 0005_order_prds despite the last created migration is 00013_etc !
I think your problem is that you are using a SQLite database, you need to use a Postgres database - see this similar question.
I am new to django and I am creating my models but I am having trouble when trying to add a foreign key to another model. here's my models:
from django.db import models
class User(models.Model):
user_id = models.CharField(max_length=10, primary_key=True)
name = models.CharField(max_length=30)
surname = models.CharField(max_length=30)
role = models.CharField(max_length=10)
address = models.CharField(max_length=50)
email = models.EmailField(max_length=30)
password = models.CharField(max_length=20)
phone = models.IntegerField()
GPA = models.FloatField(max_length=5)
Gender = models.CharField(max_length=1)
def __str__(self):
return self.user_id
class Login(models.Model):
user_id = models.ForeignKey(User, on_delete=models.CASCADE, default='00000')
email = models.EmailField(max_length=30)
password = models.CharField(max_length=20)
def __str__(self):
return self.email
When I type makemigrations I get this:
You are trying to change the nullable field 'user_id' on login to non-nullable without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows)
2) Ignore for now, and let me handle existing rows with NULL myself (e.g. because you added a RunPython or RunSQL operation to handle NULL values in a previous data migration)
3) Quit, and let me add a default in models.py
Select an option: 3
So I added a default value but I am getting this error when I tried to migrate. I tried to change user_id from User to AutoField so I don't have to add any default value but it is still giving me this error. Plus I don't know why it says user_id_id at the end. Can anyone help me out with this?
Running migrations:
Rendering model states... DONE
Applying login.0003_login_user_id...Traceback (most recent call last):
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\mysql\base.py", line 112, in execute
return self.cursor.execute(query, args)
File "C:\User\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\cursors.py", line 226, in execute
self.errorhandler(self, exc, value)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\connections.py", line 42, in defaulterrorhandler
raise errorvalue
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\cursors.py", line 223, in execute
res = self._query(query)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\cursors.py", line 379, in _query
rowcount = self._do_query(q)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\cursors.py", line 342, in _do_query
db.query(q)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\connections.py", line 286, in query
_mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (1067, "Invalid default value for 'user_id_id'")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm 5.0.4\helpers\pycharm\django_manage.py", line 41, in <module>
run_module(manage_file, None, '__main__', True)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\runpy.py", line 182, in run_module
return _run_module_code(code, init_globals, run_name, mod_spec)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:/Users/Desktop/Project/TSL/mysite\manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\management\__init__.py", line 353, in execute_from_command_line
utility.execute()
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\management\__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\management\base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\management\base.py", line 399, in execute
output = self.handle(*args, **options)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\management\commands\migrate.py", line 200, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\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 "C:\Users\AppData\Local\Programs\Python\Python35-32\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 "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\migrations\executor.py", line 198, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\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 "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\migrations\operations\fields.py", line 62, in database_forwards
field,
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\mysql\schema.py", line 50, in add_field
super(DatabaseSchemaEditor, self).add_field(model, field)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\base\schema.py", line 396, in add_field
self.execute(sql, params)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\base\schema.py", line 110, in execute
cursor.execute(sql, params)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\utils\six.py", line 685, in reraise
raise value.with_traceback(tb)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\mysql\base.py", line 112, in execute
return self.cursor.execute(query, args)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\cursors.py", line 226, in execute
self.errorhandler(self, exc, value)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\connections.py", line 42, in defaulterrorhandler
raise errorvalue
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\cursors.py", line 223, in execute
res = self._query(query)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\cursors.py", line 379, in _query
rowcount = self._do_query(q)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\cursors.py", line 342, in _do_query
db.query(q)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\connections.py", line 286, in query
_mysql.connection.query(self, query)
django.db.utils.OperationalError: (1067, "Invalid default value for 'user_id_id'")
Process finished with exit code 1
First of all, as mentioned in the comments, do NOT name the ForeignKey field user_id, but user. This will create a column called user_id in the db table and your model instance's user attribute will return a User instance while its auto-generated attribute user_id will return that user's id.
As for specifying a default value for a ForeignKey. If you do that on the model, make sure you provide an existing User. If you choose to provide a one-off default during makemigrations (if you select option 1), make sure to provide the primary key of an existing User. Alternatively, make sure there are no existing Login records in the db.
django automatically creates a primary keys for all your models, and it manages it internally.
Unless you have a very specific reason, you do not want to be managing or fiddling with the primary key manually.
Now, the specific reason for your error is that once you changed the type of field from CharField to AutoField, it changed the type from a character to an integer - but then you are assigning it a default value of a string (character), which is causing error from your database.
My best advice for you is to start from scratch. You don't need the Login model at all, django comes with authentication built-in and its own "user" model.
from django.db import models
# Renamed from User to MyUser,
# User is a built-in model that comes with django
# and is used when logging people into the system
# https://docs.djangoproject.com/en/1.9/topics/auth/default/
class MyUser(models.Model):
name = models.CharField(max_length=30)
surname = models.CharField(max_length=30)
role = models.CharField(max_length=10)
address = models.CharField(max_length=50)
email = models.EmailField() # email field doesn't need max_length
password = models.CharField(max_length=20)
phone = models.CharField(max_length=30) # this should be a CharField
GPA = models.FloatField()
gender = models.CharField(max_length=1,
choices=(('M', 'Male',),
('F', 'Female',),
('X', 'Prefer not to disclose',)))
I upgraded my django version from 1.7.5 to 1.9.2. All migration ran but when i run tests i get below error.
django.db.utils.ProgrammingError: relation "django_content_type" does not exist
I checked the database and the django_content_type model exists. I am using PostgreSQL.
I am only getting this error on PostgreSQL not in Sqlite3..
Traceback
Traceback (most recent call last):
File "/Applications/PyCharm.app/Contents/helpers/pycharm/django_test_manage.py", line 129, in
utility.execute()
File "/Applications/PyCharm.app/Contents/helpers/pycharm/django_test_manage.py", line 104, in execute
PycharmTestCommand().run_from_argv(self.argv)
File "/Users/Mac/my_env_test/lib/python2.7/site-packages/django/core/management/commands/test.py", line 30, in run_from_argv
super(Command, self).run_from_argv(argv)
File "/Users/Mac/my_env_test/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/Mac/my_env_test/lib/python2.7/site-packages/django/core/management/commands/test.py", line 74, in execute
super(Command, self).execute(*args, **options)
File "/Users/Mac/my_env_test/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/Applications/PyCharm.app/Contents/helpers/pycharm/django_test_manage.py", line 91, in handle
failures = TestRunner(test_labels, verbosity=verbosity, interactive=interactive, failfast=failfast, keepdb='--keepdb' in sys.argv)
File "/Applications/PyCharm.app/Contents/helpers/pycharm/django_test_runner.py", line 256, in run_tests
extra_tests=extra_tests, **options)
File "/Applications/PyCharm.app/Contents/helpers/pycharm/django_test_runner.py", line 156, in run_tests
return super(DjangoTeamcityTestRunner, self).run_tests(test_labels, extra_tests, **kwargs)
File "/Users/Mac/my_env_test/lib/python2.7/site-packages/django/test/runner.py", line 532, in run_tests
old_config = self.setup_databases()
File "/Users/Mac/my_env_test/lib/python2.7/site-packages/django/test/runner.py", line 482, in setup_databases
self.parallel, **kwargs
File "/Users/Mac/my_env_test/lib/python2.7/site-packages/django/test/runner.py", line 726, in setup_databases
serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),
File "/Users/Mac/my_env_test/lib/python2.7/site-packages/django/db/backends/base/creation.py", line 70, in create_test_db
run_syncdb=True,
File "/Users/Mac/my_env_test/lib/python2.7/site-packages/django/core/management/__init__.py", line 119, in call_command
return command.execute(*args, **defaults)
File "/Users/Mac/my_env_test/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/Users/Mac/my_env_test/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 172, in handle
self.sync_apps(connection, executor.loader.unmigrated_apps)
File "/Users/Mac/my_env_test/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 293, in sync_apps
cursor.execute(statement)
File "/Users/Mac/my_env_test/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Users/Mac/my_env_test/lib/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/Mac/my_env_test/lib/python2.7/site-packages/django/db/backends/utils.py", line 62, in execute
return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "django_content_type" does not exist
I used uuid field and JSON field in old Django version but in new version these fields are default provided by django models. So i replaced these fields with django default fields and created migrations again....
Now Its working without any issue..
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
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