django runserver : relation "django_migrations" already exists - django

I have a django project source code, which includes several apps. The source code have been run successfully on one environment, but when transplanted to another device, with the same postgresql version(9.4.4), python version(2.7.5), and django version(1.8.5), but the runserver reports errors like this. The database has been imported in advance.
return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "django_migrations" already exists

Try this python manage.py migrate --fake
You can read more about it in official documentation

Try troubleshooting Initial migrations using --fake-initial
python manage.py migrate --fake-initial
https://docs.djangoproject.com/en/1.8/ref/django-admin/#django-admin-option---fake-initial

If you have an empty database you can clear all your migrations and then again run migrations and migrate command.
python manage.py makemigrations
python manage.py migrate

Related

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

Syncdb not working - django cache issue

I've dropped all tables from my postgres db. Now, while running
python manage.py syncdb
I'm getting error that abc fields doesn't exist in xyz table.
It's probably some sort of django cache issue. Error is of this format:
django.db.utils.ProgrammingError: relation "mmb_data_genre" does not exist
LINE 1: ...b_data_genre"."id", "mmb_data_genre"."genre" FROM "mmb_data_...
Any suggestions how to fix this?
Note - I'm using django 1.8.2 and
python manage.py makemigratons
or
python manage.py runserver
is throwing same error.
syncdb in django 1.8 is merely an alias for the migrate command but with the additional step of creating a superuser.
Deprecated since version 1.7: This command has been deprecated in
favor of the migrate command, which performs both the old behavior as
well as executing migrations.
But syncdb (migrate) should be executed only after you have done makemigrations [app_label] but in your case you seem to have the order in reverse.
Try
./manage.py makemigrations
./manage.py migrate

django error OperationalError at /admin/blog/post/

i get this error in simple django programme run..
OperationalError at /admin/blog/post/
no such table: blog_post
if django version >=django 1.7
python manage.py makemigrations
python manage.py migrate
else
python manage.py schemamigrations
python manage.py migrate
so you need to migrate (map the model changes / new models into database tables) the changes so that your page works properly
delete the migrations folder in app and then do :
python manage.py makemigrations
python manage.py migrate
python manage.py migrate --run-syncdb
it worked for me
You need to run migrations before you can do that operation. The necessary tables don't exist in your database (as described by the error).
Check for a typo in the name of your Model. If you change your model name after makemigration and migrate, then it doesn't work and find the correct model name anymore.

Django Migrations command workflow

There are three migration commands in Django:
python manage.py makemigrations
python manage.py migrate
python manage.py syncdb
In what order do these commands should be executed in terms of workflow of a basic project?
I am using Version: 1.8
syncdb is deprecated and does the same as migrate.
Whenever you make a change to your models, and when you first create them, each time you'd want to first run makemigrations to create the migration files, then migrate to apply them to your database.