Django migration error while trying to migrate - python

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.

Related

Django: save() missing 1 required positional argument: 'self'

I'm trying to override the save method and I think I write exactly what the document said, but get the error
from django.db import models
class Post(models.Model):
name = models.CharField(max_length = 20)
lower_name = models.CharField(max_length = 20)
def save(self, *args, **kwargs):
if not self.lower_name:
self.lower_name = self.name.lower()
super().save(*args, **kwargs)
here is the traceback when I call python manage.py migrate
(project_site) λ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, works
Running migrations:
Applying works.0018_post_url_name...Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "D:\Code\Python\project_site\.venv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "D:\Code\Python\project_site\.venv\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:\Code\Python\project_site\.venv\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "D:\Code\Python\project_site\.venv\lib\site-packages\django\core\management\base.py", line 364, in execute
output = self.handle(*args, **options)
File "D:\Code\Python\project_site\.venv\lib\site-packages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "D:\Code\Python\project_site\.venv\lib\site-packages\django\core\management\commands\migrate.py", line 234, in handle
fake_initial=fake_initial,
File "D:\Code\Python\project_site\.venv\lib\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 "D:\Code\Python\project_site\.venv\lib\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 "D:\Code\Python\project_site\.venv\lib\site-packages\django\db\migrations\executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "D:\Code\Python\project_site\.venv\lib\site-packages\django\db\migrations\migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "D:\Code\Python\project_site\.venv\lib\site-packages\django\db\migrations\operations\fields.py", line 112, in database_forwards
field,
File "D:\Code\Python\project_site\.venv\lib\site-packages\django\db\backends\sqlite3\schema.py", line 327, in add_field
self._remake_table(model, create_field=field)
File "D:\Code\Python\project_site\.venv\lib\site-packages\django\db\backends\sqlite3\schema.py", line 188, in _remake_table
self.effective_default(create_field)
File "D:\Code\Python\project_site\.venv\lib\site-packages\django\db\backends\base\schema.py", line 233, in effective_default
return field.get_db_prep_save(self._effective_default(field), self.connection)
File "D:\Code\Python\project_site\.venv\lib\site-packages\django\db\backends\base\schema.py", line 212, in _effective_default
default = field.get_default()
File "D:\Code\Python\project_site\.venv\lib\site-packages\django\db\models\fields\__init__.py", line 797, in get_default
return self._get_default()
TypeError: save() missing 1 required positional argument: 'self'
python version: 3.6.8 django version: 2.2.4

timezone() missing required argument 'offset' (pos 1) in models.py while trying to migrate, what is the reason?

while making a blog, my models.py:
from django.db import models
class Post(models.Model):
title=models.CharField(max_length=200,blank=True)
author=models.ForeignKey('auth.user',on_delete=models.CASCADE,)
image=models.ImageField(upload_to='media/',blank=True)
content=models.CharField(max_length=1000,blank=True)
date=models.DateTimeField(auto_now_add=False, editable=False)
tag=models.CharField(max_length=100,blank=True)
slug=models.CharField(max_length=200,blank=True)
def __str__(self):
return self.title
when i run python manage.py makemigrations, datefield is created in database:
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('blog', '0013_auto_20191124_1448'),
]
operations = [
migrations.AlterField(
model_name='post',
name='date',
field=models.DateTimeField(editable=False),
),
]
but when i try to run python manage.py migrate, it shows the following error:
(Django-k7xSBAPV) C:\Myfiles\python\Django\myblog\blog_project>python manage.py
makemigrations
No changes detected
(Django-k7xSBAPV) C:\.....\blog_project>python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, blog, contenttypes, sessions
Running migrations:
Applying blog.0004_post_date...Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "C:\Users\ronyrocks\.virtualenvs\Django-k7xSBAPV\lib\site-
packages\django\core\management\__init__.py", line 381, in
execute_from_command_line
utility.execute()
File "C:\Users\ronyrocks\.virtualenvs\Django-k7xSBAPV\lib\site-
packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\ronyrocks\.virtualenvs\Django-k7xSBAPV\lib\site-
packages\django\core\management\base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\ronyrocks\.virtualenvs\Django-k7xSBAPV\lib\site-
packages\django\core\management\base.py", line 364, in execute
output = self.handle(*args, **options)
File "C:\Users\ronyrocks\.virtualenvs\Django-k7xSBAPV\lib\site-
packages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\ronyrocks\.virtualenvs\Django-k7xSBAPV\lib\site-
packages\django\core\management\commands\migrate.py", line 234, in handle
fake_initial=fake_initial,
File "C:\Users\ronyrocks\.virtualenvs\Django-k7xSBAPV\lib\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\ronyrocks\.virtualenvs\Django-k7xSBAPV\lib\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\ronyrocks\.virtualenvs\Django-k7xSBAPV\lib\site-
packages\django\db\migrations\executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\ronyrocks\.virtualenvs\Django-k7xSBAPV\lib\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\ronyrocks\.virtualenvs\Django-k7xSBAPV\lib\site-
packages\django\db\migrations\operations\fields.py", line 112, in database_forwards field,
File "C:\Users\ronyrocks\.virtualenvs\Django-k7xSBAPV\lib\site-
packages\django\db\backends\sqlite3\schema.py", line 327, in add_field
self._remake_table(model, create_field=field)
File "C:\Users\ronyrocks\.virtualenvs\Django-k7xSBAPV\lib\site-
packages\django\db\backends\sqlite3\schema.py", line 188, in _reremake_table
self.effective_default(create_field)
File "C:\Users\ronyrocks\.virtualenvs\Django-k7xSBAPV\lib\site-
packages\django\db\backends\base\schema.py", line 233, in effectctive_default
return field.get_db_prep_save(self._effective_default(field), self.connection)
File "C:\Users\ronyrocks\.virtualenvs\Django-k7xSBAPV\lib\site-
packages\django\db\backends\base\schema.py", line 212, in _effecective_default
default = field.get_default()
File "C:\Users\ronyrocks\.virtualenvs\Django-k7xSBAPV\lib\site-
packages\django\db\models\fields\__init__.py", line 801, in get_t_default
return self._get_default()
TypeError: timezone() missing required argument 'offset' (pos 1)
but without date in models.py, everything was working fine. i tried to use dafault=timezone.now() along with from django.utils.timezone import * but it's showing different warning.
please give me some suggestions. i am doing this in django2.2.
i think i solved the problem. i am sharing it if anyone wish to look through.... and i have polished the code...
from django.db import models
from django.utils import timezone
class Post(models.Model):
title=models.CharField(max_length=200)
author=models.ForeignKey(
'auth.User',on_delete=models.CASCADE,)
image=models.ImageField(upload_to='media/',blank=True)
content=models.CharField(max_length=1000)
date=models.DateTimeField(default=timezone.now)
tag=models.CharField(max_length=100)
slug=models.CharField(max_length=200)
def __str__(self):
return self.title
def publish_time(self):
publication=timezone.now()
self.save()
My main issue was i forget to migrate when i first created the app. i am not sure this is the main reason or not. but it worked smoothly.besides i have decided to use ckeditor, but it poses another problem which it just accepts picture from the link. that's why i prefer more ImageField....
any way this model.py file ran at first attempt while starting a clean app.
thanx again

Getting Migrate Error on Model Field that doesn't exist

Ran my server today and realized I had 'two unapplied migrations'. I ran the command makemigrations and it detected no change, so naturally I then ran migrate.
I get this error code on my terminal:
Applying Campaigns.0016_campaignrecruit_date...Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py", l
ine 371, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py", l
ine 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/base.py", line
288, in run_from_argv
self.execute(*args, **cmd_options)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/base.py", line
335, in execute
output = self.handle(*args, **options)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/commands/migrat
e.py", line 200, in handle
fake_initial=fake_initial,
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/migrations/executor.py", lin
e 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/migrations/executor.py", lin
e 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/migrations/executor.py", lin
e 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/migrations/migration.py", li
ne 122, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/migrations/operations/fields
.py", line 84, in database_forwards
field,
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py",
line 306, in add_field
self._remake_table(model, create_field=field)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py",
line 178, in _remake_table
self.effective_default(create_field)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/base/schema.py", li
ne 240, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/fields/__init__.py",
line 767, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/fields/__init__.py",
line 1276, in get_db_prep_value
value = self.get_prep_value(value)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/fields/__init__.py",
line 1271, in get_prep_value
return self.to_python(value)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/fields/__init__.py",
line 1233, in to_python
parsed = parse_date(value)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/dateparse.py", line 74, i
n parse_date
match = date_re.match(value)
TypeError: expected string or bytes-like object
So to me, it looks like a date field in my model campaignrecruit is getting an input that it doesn't know how to handle. Which I would understand. But the thing is I don't have a datefield in my campaignrecruit model. The Time field is intentionally commented out right now.
Here's my models.py:
class campaignrecruit(models.Model):
campaign = models.ForeignKey(startcampaign, on_delete=models.CASCADE)
volunteer = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
join = models.BooleanField()
#Time = models.IntegerField()
def __str__(self):
return str(self.volunteer)
What else would throw this error? Any and all help is appreciated.
Since no one else answered, I will. As Aritra stated, I totally forgot about the migration files that are created whenever you run the migrate command. I had removed these model fields from my models and whenever I migrated again there was an error removing the files in my migrations file. After manually going in and removing the files, my error was fixed.
The database was just out of sync with what django had in its files.

Django migrations mixed up with 'PROTECT' and 'CASCADE'

I am stuck with migrations in django. I have two really basic models that will not migrate:
from django.db import models
# Create your models here.
class Instanz(models.Model):
type = models.CharField(max_length=30)
angelegt_am = models.DateField(auto_now_add=True)
class Person(models.Model):
instanz_fk = models.ForeignKey('Instanz', on_delete=models.CASCADE)
last_name = models.CharField(max_length=30)
first_name = models.CharField(max_length=30)
geburtsdatum = models.DateField()
It will raise the following exception. I dont get why it will search for a field named PROTECT. I used models.PROTECT in earlier migrations, before switching to CASCADE, but not any longer...
Operations to perform:
Apply all migrations: admin, auth, contenttypes, kundencenter, sessions
Running migrations:
Applying kundencenter.0001_initial...Traceback (most recent call last):
File "C:\Users\Micha\Dropbox\PycharmProjects\vertriebstool_virtualenv\lib\site-packages\django\db\models\options.py", line 617, in get_field
return self.fields_map[field_name]
KeyError: 'PROTECT'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Micha\Dropbox\PycharmProjects\vertriebstool\manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\Micha\Dropbox\PycharmProjects\vertriebstool_virtualenv\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
utility.execute()
File "C:\Users\Micha\Dropbox\PycharmProjects\vertriebstool_virtualenv\lib\site-packages\django\core\management\__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Micha\Dropbox\PycharmProjects\vertriebstool_virtualenv\lib\site-packages\django\core\management\base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\Micha\Dropbox\PycharmProjects\vertriebstool_virtualenv\lib\site-packages\django\core\management\base.py", line 345, in execute
output = self.handle(*args, **options)
File "C:\Users\Micha\Dropbox\PycharmProjects\vertriebstool_virtualenv\lib\site-packages\django\core\management\commands\migrate.py", line 204, in handle
fake_initial=fake_initial,
File "C:\Users\Micha\Dropbox\PycharmProjects\vertriebstool_virtualenv\lib\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 "C:\Users\Micha\Dropbox\PycharmProjects\vertriebstool_virtualenv\lib\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 "C:\Users\Micha\Dropbox\PycharmProjects\vertriebstool_virtualenv\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\Micha\Dropbox\PycharmProjects\vertriebstool_virtualenv\lib\site-packages\django\db\migrations\migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:\Users\Micha\Dropbox\PycharmProjects\vertriebstool_virtualenv\lib\site-packages\django\db\migrations\operations\models.py", line 96, in database_forwards
schema_editor.create_model(model)
File "C:\Users\Micha\Dropbox\PycharmProjects\vertriebstool_virtualenv\lib\site-packages\django\db\backends\base\schema.py", line 246, in create_model
definition, extra_params = self.column_sql(model, field)
File "C:\Users\Micha\Dropbox\PycharmProjects\vertriebstool_virtualenv\lib\site-packages\django\db\backends\base\schema.py", line 136, in column_sql
db_params = field.db_parameters(connection=self.connection)
File "C:\Users\Micha\Dropbox\PycharmProjects\vertriebstool_virtualenv\lib\site-packages\django\db\models\fields\related.py", line 940, in db_parameters
return {"type": self.db_type(connection), "check": self.db_check(connection)}
File "C:\Users\Micha\Dropbox\PycharmProjects\vertriebstool_virtualenv\lib\site-packages\django\db\models\fields\related.py", line 937, in db_type
return self.target_field.rel_db_type(connection=connection)
File "C:\Users\Micha\Dropbox\PycharmProjects\vertriebstool_virtualenv\lib\site-packages\django\db\models\fields\related.py", line 855, in target_field
return self.foreign_related_fields[0]
File "C:\Users\Micha\Dropbox\PycharmProjects\vertriebstool_virtualenv\lib\site-packages\django\db\models\fields\related.py", line 595, in foreign_related_fields
return tuple(rhs_field for lhs_field, rhs_field in self.related_fields if rhs_field)
File "C:\Users\Micha\Dropbox\PycharmProjects\vertriebstool_virtualenv\lib\site-packages\django\db\models\fields\related.py", line 582, in related_fields
self._related_fields = self.resolve_related_fields()
File "C:\Users\Micha\Dropbox\PycharmProjects\vertriebstool_virtualenv\lib\site-packages\django\db\models\fields\related.py", line 575, in resolve_related_fields
else self.remote_field.model._meta.get_field(to_field_name))
File "C:\Users\Micha\Dropbox\PycharmProjects\vertriebstool_virtualenv\lib\site-packages\django\db\models\options.py", line 619, in get_field
raise FieldDoesNotExist('%s has no field named %r' % (self.object_name, field_name))
django.core.exceptions.FieldDoesNotExist: Instanz has no field named 'PROTECT'
Here's the deal: I messed up some earlier migrations that were blocking the process, because Djangos migrate uses all unaplied migrations.
I had to go to the folder \\migrations and delete all files (except init.py of course).
Everything works like a charm now.
If this happens in production, you would probably delete only the migrations to the point of the last working migration...

Updating the models in django

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?

Categories