OperationalError occurred while adding new fields in Django - python

I have a web app I've been managing for months, and I attempted to make some updates on my app. I added new fields to the existing class, but django raised an error.
These are what I've done.
1. Added a new field to an existing model.
class ExistingModel(models.Model):
# Existing Fields
new_field = models.IntegerField()
2. Attempted to make migration
$ python manage.py makemigrations
However, it raised django.db.utils.OperationalError: no such column during this process.
I added django.contrib.sites to INSTALLED_APPS in settings.py.
My django version is 2.0.3.
How can I add new fields to the existing model?

Related

How do I add another field to my model without getting an error?

I added another field to my Listing() model called highest_bid. However, when I try to look at the listing model in the /admin page I get the OperationalError:
no such column: auctions_listing.highest_bid
After adding this field I tried using makemigrations, but it said that there were no changes and I also tried using migrate but that also said that there was nothing to migrate.
I also tried removing null=True on the field but that did not change anything.
How do I add this field without getting an error?
models.py:
class Listing(models.Model):
...
highest_bid = models.FloatField()
# highest_bid = models.FloatField(null=True)
# they both returned the same error
According to this answer you have to make sure you:
Add your app to INSTALLED_APPS inside settings.py, otherwise makemigrations will not check for changes in your models
Run ./manage.py makemigrations <app_name> if your app doesn't have a migrations module yet (i.e. it's going to be your app's 0001_initial.py migration)
If you can cross these two requirements off your list (i.e. you're doing subsequent changes to your models), then ./manage.py makemigrations without any <app_name> should simply work.
how makemigrations didn't add the changes, you have to find a way to implement make migrations and create a file in the migrations folder otherwise, your field will not be in the DB and you will keep seeing the same error
so try ./manage.py makemigrations your_app_name

Django dropped table still exists

Having problems with a model in one of my django apps.
I can make an instance of the model, but when I go to access it, I get an error:
# create and save model instance
user_form = UserSearchForm(user=user)
user_form.save()
# Then later
first_user_form = UserSearchForm.objects.all()[0]
(1146, "Table 'project.app_tablename' doesn't exist")
Request Method: GET
Request URL: http://10.0.75.1:8000/search/
Django Version: 1.9.5
Exception Type: ProgrammingError
Exception Value:
(1146, "Table 'project.app_tablename' doesn't exist")
Exception Location: /opt/project-venv/lib/python3.4/site-packages/MySQLdb/connections.py in query, line 280
Python Executable: /opt/project-venv/bin/python
Python Version: 3.4.3
Python Path:
['/opt/project/project',
'/opt/project-venv/lib64/python34.zip',
'/opt/project-venv/lib64/python3.4',
'/opt/project-venv/lib64/python3.4/plat-linux',
'/opt/project-venv/lib64/python3.4/lib-dynload',
'/usr/lib64/python3.4',
'/usr/lib/python3.4',
'/opt/project-venv/lib/python3.4/site-packages']
Server time: Tue, 28 Mar 2017 22:50:16 +0000
I'm using Django 1.95 and I tried the following from the linked answer (https://stackoverflow.com/a/36565161/2516846):
Delete app migrations folder
Drop all app tables, e.g. DROP TABLE search_usersearchform
Delete all migrations from db: DELETE FROM django_migrations WHERE app = 'search'.
python manage.py makemigrations search
python manage.py migrate
But when I run migrate, it gives error:
django.db.utils.ProgrammingError: relation "search_usersearchform" already exists
Summary: Cannot create model again as relation already exists, yet cannot access model in my app as it says table doesn't exist
Is there a reference to this table/model stored else where which I need to remove? I don't know how the relation can already exist if I dropped the table, deleted migrations folder and removed migrations from db
EDIT
Okay finally got this sorted.
My project had two databases, the 'default' db, using postgres, and the 'secondary' db, using mysql.
When I created a new model in my app, and ran the migration, the table was added to the default db.
This allowed me to create an instance of the new model.
However when I went to access a previously saved instance, I got the error saying the table does not exist.
Turns out it was caused by my apps router.py, which redirected any queries to the 'secondary' db, which of course did not have the table.
To fix this, I added conditionals to the db_for_write and db_for_read methods in router.py, to route those models to the 'default' db.
Try deleting or changing migration files that have that model in them, care with the dependencies. Then run again python manage.py makemigrations and python manage.py migrate it should create the model again. (We are talking about your test DB right?)
make a back up of the models.py, delete the model code from the original models.py, then run:
python manage.py makemigrations
python manage.py migrate
Then simply restore the model code in models.py and run the above commands again.
Following these steps should fix it:
Remove the database.
Create an empty one with the same name.
Optionally, you may need to create a superuser for the new database.
Remove the content of package migrations (not the entire package), except file init.py.
Run command python manage.py makemigrations
Run command python manage.py migrate

Migrating from auth_user in Django

I created a custom user model in Django and it worked up fine. However, I decided to create a custom model to suit my needs after the project was up and running.
As a result, I will need to migrate the schema (Currently, when I register a user, the code is still referencing to the the auth_user database tables where as the new custom user table is user.)
I have set the AUTH_USER_MODEL in settings.py to userapp.User, where userapp is my custom user app and User is the Model that inherits from the AbstractUser model.
I am fairly new to Django and cannot understand how to achieve this. One obvious way to clean install the database, which is not something that I'm looking to do as it will remove all my data.
How do I migrate then? I've heard South is used for that but I don't know how to use it. Besides I think South isn't required in the recent versions of Django.
My version of Django is 1.8.2.
I did this recently - we used a data migration to move between the two models. Rough steps:
Create the new user model (without telling Django about it), make/apply migrations to create the database table(s)
Write a the data-migration to copy the data over to the new user model. Then run this migration and update Django to use the new model
Now you should be switched over, and can delete/archive the old auth_user table as needed

Django 1.7.3 - Lookup failed for model referenced by field

I am trying create a new model with Django, but I keep running into the error Lookup failed for model referenced by field help.HelpDefinition.org: account.Organization. Organization has been imported. You can see the model below.
models.py
org = models.ForeignKey(Organization, unique=True)
help_type = models.CharField(max_length=255, choices=HELP_CHOICES)
help_content = models.TextField(blank=True)
This model has been successfully migrated previously. I dropped the table via psql in Postgres so that it could be recreated.
It happens when you change the target objects in relationship. Even if they have the same name and fields they are not the same objects. I had the same issue and deleting all previous migrations from migrations folder solved it.
You can also add as a dependency to the migration the last migration from the object's app. That did the trick for me.
class Migration(migrations.Migration):
dependencies = [
(<app>, <last_migration_filename>),
...
My case was: moving away from South I deleted almost all migration files from several apps and applied makemigrations and migrate and later on I found out some forgotten migrations in one app and I tried to do the process (delete/makemigrations) only for this one app. But going back one step and recreating the migrations for ALL the apps fixed the issue for me.

Change to Django's User Model - Migration issues

I have used my own user model till now and suddenly i realized that django's user model offers me exactly the things that i would have to create on my own... why not to take django's user model..
now i changed the user model and made it inherit from django user model by adding:
class User(User)..
and tried to migrate the db with these steps:
1. python manage.py schemamigration home --auto
-- it asked me to give some default values for these two columns I mean down here
2. python manage.py migrate home
3. error
3.error says that user_ptr_id exists double and thats why couldnot create the unique index user_pkey.
what can i do now so that i can migrate?

Categories