docker-compose error when deploying portainer - python

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/.

Related

Django- Why I am getting this Programming Error

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.

django migrate "No migrations to apply."

I kind of know why when I do migrate it gives me the message of no migrations to apply but I just don't know how to fix it
This is what happens.
I added a new field named update into my model fields.
I did a migration which created a file called 0003_xxxxx.py then I did a migrate now this worked fine.
But then because of some reason, I have to remove the update field from the same model.
So I removed it (more likey commented instead of really delete the code) then I did migration and migrate which removed the field in db. (created 0004_xxxxx.py)
But sigh....some reason I have to add the field back again (this is why I only commented out) but then before I do the migration
I removed the 0003_xxxx.py and 0004_xxxx.py files I wanted to remove those two files because this is actually the same or almost the same config as 0003_xxxx.py so I feel it's pointless having the 0003_xxxx.py and 0004_xxxx.py here...also when I go production, that's just another extra step for python to run.
After I removed those two files, I did the migration which creates another 0003_xxxx.py but when I do migrate it gives me the message of no migrations to apply
I know that by deleting the 0003_xxxx.py and get the original 0003 and 0004 back then do another migration (creates 0005_xxxx.py) then migrate then changes will be made. I know this because I didn't really delete the original 0003 and 0004 I moved it somewhere just in case of this kind of happening.
But why is this though? and is there a way to fix it?
Thanks in advance for any replies
django keep records of current migrations log by table django_migrations in your db.
something like:
The migrations is a chain structure,it's depend on the parent node.By this table django can know which migrations file is executed.
In you case,no migrations to apply because the new create 0003_xxxx.py is record in this table,you can fix it by delete this record in this table.
If you want remove some migration file you need see Squashing migrations.
Or even simply just delete the last migration row from your database you can try the following
First, check which row you want to remove
SELECT * FROM "public"."django_migrations";
Now pick the last id from the above query and execute
DELETE FROM "public"."django_migrations" WHERE "id"=replace with your Id;
run ./manage.py migrate.
just run:
python manage.py run --run-syncdb

Django with Postgres - ProgrammingError, migrations not updating the database

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.

How to create a new table using model

So I have this django installation in which there are a bunch of migration scripts. They look like so:
00001_initial.py
00002_blah_blah.py
00003_bleh_bleh.py
Now I know these are "database building" scripts which will take stuff defined in models.py and run them against the db to "create" tables and stuff.
I want to create a new table(so I created its definition in models.py). For this, I have copied another model class and edited its name and fields and it is all fine. Lets call this new model class 'boom'.
My question is now how do I "create" this boom table using the migration script and the boom model?
I am worried that I might accidentally disrupt anything that is already in DB. How do I run the migration to create only boom table? How do I create a migration script specifically for it?
I know that it has something to do with manage.py and running migrate or runmigration (or is it sqlmigrate?...im confused). While creating the boom table, I dont want the database to go boom if you know what I mean :)
First, create a backup of your database. Copy it to your development machine. Try things out on that. That way it doesn't matter if it does go "boom" for some reason.
The first thing to do is
python manage.py showmigrations
This shows all the existing migrations, and it should show that they have been applied with an [X].
Then,
python manage.py makemigrations
Makes a new migration file for your new model (name 00004_...).
Then do
python manage.py migrate
to apply it. To undo it, go back to the state of migrations 00003, with
python manage.py migrate <yourappname> 00003
There are two steps to migrations in Django.
./manage.py makemigrations
will create the migration files that you see - these describe the changes that should be made to the database.
You also need to run
./manage.py migrate
this will apply the migrations and actually run the alter table commands in SQL to change the actual database structure.
Generally adding fields or tables won't affect anything else in the database. Be more careful when altering or deleting existing fields as that can affect your data.
The reason for two steps is so that you can make changes on a dev machine and once happy commit the migration files and release to your production environment. Then you run the migrate command on your production machine to bring the production database to the same state as your dev machine (no need for makemigrations on production assuming that your databases started the same).
My question is now how do I "create" this boom table using the
migration script and the boom model?
./manage.py makemigrations
I am worried that I might accidentally disrupt anything that is
already in DB.
The whole point of migrations, is that it doesn't
I know that it has something to do with manage.py and running migrate
or runmigration
For more information please refer to : https://docs.djangoproject.com/en/1.10/topics/migrations/
And rest assured that your database will not go boom! :-)
I solved it simply, changing the name of the new model to the original name, and then I checked if there is the table in the database, if not, I just create a new table with the old name with just a field like id.
And then clear migrations and create new migrations, migrate and verify table was fixed in DB and has all missing fields.
If it still doesn't work, then change the model name back to a new one.
but when django asks you if you are renaming the model you should say NO to get the old one removed properly and create a new one.
This type of error usually occurs when you delete some table in dB manually, and then the migration history changes in the tables are lost.
But it is not necessary to erase the entire database and start from scratch.

Why is Django creating my TextField as a varchar in the PostgreSQL database?

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.

Categories