Django: CommandError: App 'zero' does not have migrations - django

I trying to revert the migrations of my Django application. I have five migrations which are synced with the database. I use the following command:
$ python manage.py migrate zero
This fails with the following error message:
CommandError: App 'zero' does not have migrations (you cannot selectively sync unmigrated apps)

You should provide the app label to migrate:
python manage.py migrate myapp zero

Related

Django 2.1 NOT NULL Constraint Failed When Creating Super User

I've been using Django for several months now without any issue but when I went to create a new project and add the super-user, I get the following error:
django.db.utils.IntegrityError: NOT NULL constraint failed: auth_user.last_login
All migrations have been created and applied successfully. I have destroyed and recreated the database a half dozen times to no avail. App migrations run without any problem whatsoever and this is the first time I've encountered this issue in an Django project.
I just realized this message was buried in the dozens of lines of error messages:
You have 12 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
Running just manage.py migrate and/or manage.py migrate appname did not apply the required system migrations. I had to do each one individually this time for some reason.
After running each one individually I was able to create a superuser without a problem:
manage.py migrate admin
manage.py migrate auth
manage.py migrate contenttypes
manage.py migrate sessions
I've never had to do that before, however. If anyone knows a reason as to why that happened I'd love to know but I've solved the main issue for now.
I'm too used
manage.py migrate admin
manage.py migrate auth
manage.py migrate contenttypes
manage.py migrate sessions
this helped for me
python manage.py createsuperuser

How can reset django apps which migration folder was deleletd?

In my django app path, the migrations folder was deleted, but I want to recover it, when executing migrate command:
python manage.py migrate --fake api.desktops
CommandError: App 'api.desktops' does not have migrations (you cannot selectively sync unmigrated apps)
How can reset those app?
If your database and and app are synchronised you should be able to do the following:
./manage makemigrations <app_name>
./manage migrate <app_name> --fake
If the database is out of sync you can also perform the same as above but will need to manually alter your database to align with the new migration structure.

Migration in Postgresql and Django 1.8

In my project I am using django 1.8 and for a fresh project if I run
python manage.py runserver
it shows the following message:
You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
Then if i run the command
python manage.py migrate
it works fine for sqlite.
But if i connect with postgresql in my local_settings.py and run the above migration command then it gives the following error:
django.db.utils.ProgrammingError: relation "django_content_type" does not exist
You probably have an app that has a (generic) foreign key to ContentType. This causes migrations to fail because the database tries to create a foreign key to a table that doesn't exist yet. Try migrating contenttypes first with python manage.py migrate contenttypes and then applying your other migrations.

Recreating a database for an existing django schema

I've dropped my Postgres database by accident. Then as per this solution I deleted the migration files but now can't execute the third step's command "python manage.py migrate --fake".
RuntimeError: Error creating new content types.
Please make sure contenttypes is migrated before trying to migrate apps individually.
psycopg2.ProgrammingError: relation "django_content_type" does not exist
LINE 1: ..."."app_label", "django_content_type"."model" FROM "django_co...
^
I tried running "python manage.py migrate contenttypes" and "python manage.py makemigrations contenttypes --empty" but neither work.
Using django 1.9.5
Synchronise your project first:
like this:
python manage.py syncdb
when finish you can run: python manage.py makemigrations and last python manage.py migrate

Django 1.9 with CORS dumping data: "corsheaders_corsmodel" does not exist

I'm developing a Django (1.9) Rest backend and AngularJS frontend with Cross-site referencing. While attempting to execute a ./manage.py dumpdata command, it throws the following exception:
$ python manage.py dumpdata -o dev/dumpdata.json
CommandError: Unable to serialize database: relation
"corsheaders_corsmodel" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "corsheaders_corsmodel"
Any idea how to handle?
I had this same problem, and resolved it by invoking python manage.py makemigrations specifically for the corsheaders app:
$ python manage.py makemigrations corsheaders
$ python manage.py migrate
I think what happened in my case was that, after an upgrade from Django 1.8 to 1.9, the initial migration was never applied when I updated my DB.
I tracked it down by noticing that the corsheaders app was not listed in the Apply all migrations output of python manage.py migrate:
$ python manage.py migrate
Operations to perform:
Apply all migrations: sessions, admin, xyz, auth, contenttypes
Running migrations:
No migrations to apply.
Yet running a manual migration for corsheaders actually creates the initial migration:
$ python manage.py makemigrations corsheaders
Migrations for 'corsheaders':
0001_initial.py:
- Create model CorsModel
After having done that, a migrate does show corsheaders in the output, and successfully applies the migration as expected:
$ python manage.py migrate
Operations to perform:
Apply all migrations: corsheaders, sessions, admin, xyz, auth, contenttypes
Running migrations:
Rendering model states... DONE
Applying corsheaders.0001_initial... OK
If corsheaders_corsmodel table does not exist, then there's no data to dump.
So you can just run:
$python manage.py dumpdata --exclude=corsheaders
I had the same problem and I solve it this way.