Recently, I changed a model called 'Objects' by adding a 'description' field (which has null set to True). Ever since I migrated to the Postgres database hooked up to the Django project, I have received the following error whilst trying to access the 'Objects' model.
ProgrammingError at /objects/results/
column objects_objects.description does not exist
LINE 1: ...1, "objects_objects"."mapped_product_id" AS Col2, "objects_o...
Objects model...
class Objects(models.Model):
mapped_product = models.ForeignKey(Product,related_name='p_objects',verbose_name='Product',null=True,blank=True,on_delete=models.SET_NULL)
description = models.CharField(max_length=1000,null=True,blank=True)
slug_key=models.SlugField(unique=True)
is_active=models.BooleanField(verbose_name='Active',default=False)
In the console, Django says migrations are all successful, and when running the local server it does not show that there are unapplied migrations.
Any ideas on how to sync the database up with the models from scratch? I do not mind clearing my development database for this, and have already tried removing all of the previous migrations.
I understand that this is somewhat vague at this point since I'm unsure what code I should post here - let me know if there is some code I should post here.
Related
I was deploying django with Portainer.
While deploying, the following error occurred in django image log.
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration account.0001_initial is applied before its dependency users.0001_initial on database 'default'.
I deleted the migrations file and tried to migrate again and deploy, but the same error occurred.
maybe I think the problem is probably caused by customizing the User model.
What should I do?
First of all, you can not simply delete your migration files like that because the migration state is stored in your database, not only just in your migration files.
Open your database and check your django_migrations table and you will understand where your error come from. The error message means that Django find out the migration account.0001_initial existed on django_migrations table without users.0001_initial, but your migration files defined that users.0001_initial has to be before the account.0001_initial. So it don't know how to process the confliction.
If your data is not important, simply drop your database and create a new one will help. Secondly, you will have to follow the process to reverse a migration instead of deleting it here https://docs.djangoproject.com/en/3.2/topics/migrations/#reversing-migrations
I recommend you to read the whole https://docs.djangoproject.com/en/3.2/topics/migrations/.
Django- Why I am getting this Programming Error When I have not declared shop_product variable anywhere .
Please help Click here to view error Image
please refer to this image of error
shop_product is the name of the database table for the model Product in the application shop.
Most likely cause for this error is that you didn't apply database migrations, or, if you did, that you didn't add the application shop to your INSTALLED_APPS.
Update:
According to one of your comments, you are trying to use SQLite, which you cannot use on Heroku, see https://devcenter.heroku.com/articles/sqlite3
But it seems you figured that out because according to the settings of your app, you are using PostgreSQL, but you have not applied your migration.
Migrations are created once with manage.py makemigrations, but you have to apply them on every database, i.e. both on your local dev environment and on the database your application running on Heroku uses. For the latter, try this:
heroku run python manage.py
From the partial SQL query in the image it seems that "shop_product" is a table.
Note:
LINE 1: ... "shop_product"."id" FROM "shop_product"
Check your models if you have ShopProduct model, and check if you have migrations are updated.
Check if you have ManyToMany fields that might create that table, and also check if migrations are up to date.
I have two Django apps. A, the core app, and B, which started out as an individual app, but actually fits better within A as we later discovered. I'm able to move all the code from B into A, but falling short when it comes to the database.
B will be deleted, so it'd be best if the migration lived entirely in A. That way, anyone can pull the latest code and run manage.py migrate and it will work on local machines even after B is deleted.
However, the data still needs to be migrated from B to A when running the migration on the production database.
My attempts
I tried inlining B's models in the migration, querying them (if they exist), then saving new objects using the new models. This is a technique I've used with SQLAlchemy before. The only problem is that Django then adds these temporary models to django_contenttype and tells me "The following content types are stale and need to be deleted" the next time I run a migration. (I also don't know if Django does anything else I might have missed.)
I also tried setting db_table to the old app's database table as suggested here, but then the table doesn't exist when running locally from scratch. If I include a CreateModel migration, then I get django.db.utils.OperationalError: table "b_model" already exists when migrating on an existing database.
Any ideas?
Django 1.7, Python 3.4.
In my models I have several TextFields defined.
When I go to load a JSON fixture (which was generated from an SQLite3 dump), it fails on the second object, which has 515 characters for one of its fields.
The error printed is
psycopg2.DataError: value too long for type character varying(500)
I created a new database (not just a table drop, a whole new db), modified my settings.py file, ran manage.py syncdb on the new database, created a user, and tried to load the data again, getting the same error.
Upon opening pgAdmin3, all columns, both CharField and TextField defined are listed as type character var.
So it seems TextField is being ignored and CharFields are being created instead. The PostgreSQL documentation explicitly lists both text and character types, and defines text as being unlimited in length. Any idea why?
I'm not sure what the exact cause was, but it seems to be related to django's migration tool storing migrations, even on a new database.
What I did to get this behavior:
Create django project, then apps, using CharField
syncdb, run the project's dev server
kill the devserver, modify fields to be TextField
Create a new Postgres database, modify settings.py
Run syncdb, attempt to load fixtures
See the error in question, examine db instance
What fixed the problem:
Create a new database, modify settings.py
delete all migrations in apps/migrations folders
after running syncdb, also run createmigrations and migrate
The last step generated a migration, even though there were none stored in the migrations folder, and there had been no changes to models or data since syncdb was run on the new database, which I found to be odd.
Somewhere in the last two steps this was fixed. Future people stumbling upon this: sorry, I'm not going to keep creating django projects to test the behavior further, but perhaps with this information you can fix your own database problems.
I'm trying to deal with a very puzzling error in a Django app. When DEBUG=False, trying to delete a user (via user.delete()) gives this database error:
DatabaseError: relation "social_auth_usersocialauth" does not exist
LINE 1: ...", "social_auth_usersocialauth"."extra_data" FROM "social_au...
However, I do not have social_auth or anything by a similar name in INSTALLED_APPS, nor are there any such tables in my database, nor does any of my code reference anything of the sort (I ran a text search on 'social' in the entire project folder) - and again, this works fine when DEBUG=True. social_auth is installed on my system and on my PYTHONPATH, but I cannot see where this app is getting the idea it should be having social_auth's tables in its database, let alone why it only thinks so when DEBUG=False.
What possible pathways could my app be getting this table from and how could I convince it it's not supposed to be there?
The problem could be caused by saved generic relations realized by Django content types. Relations in Django are not only static, implemented by models and INSTALLED_APPS but also dynamic implemented by table django_content_type that saves mapping from a numeric id to app_label + model. An example of possible dynamic relationship is a permission or a comment. You can have or have not a permission to any table of any installed application. You can write a comment to everything e.g to an article, to a user to a comment itself without changing any model. This relation is realized by saving numeric id of ContentType related to that model (table) and a primary key of related object (row).
Django does not expect that someone can manipulate the database manually. If you use south for manipulation then if you after uninstalling an application then run syncdb, you are asked by south if you want automatically remove orphant content types. Then can be unused tables removed securely without beeing later referenced.
(Possible hack: delete from django_content_type where app_label='social_auth' but south is unfallible.)
Many parts of the question are still open.
Edit:
Why it was not the right way: All generic relations are from descendants to the parent and all data about the relation are saved in descendant. If the child app is removed from INSTALLED_APPS then django.db code can nevermore try to remove descendants because it can not recognize which columns contain the relation data.
This table is created by django-social-auth application.
Looks like you've added it to your project and haven't launched migrate (or syncdb).