简体   繁体   中英

Django migration relation does not exist

So I'm trying to run the initial migrations on a django app and when I try to run the migrate command (python manage.py migrate or makemigrations) I get the following error:

psycopg2.ProgrammingError: relation "dotworks_server_internship" does not exist
LINE 1: ...s", "dotworks_server_internship"."questions" FROM "dotworks_...
                                                             ^

I'm on a Windows environment using Django 1.9.6 and my database is postgres. Plus, I'm using PGAdmin to manage my database.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'dotworks',
        'USER': 'postgres',
        'PASSWORD': 'mypasswordgoeshere',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

I had this problem and I had to comment out everything in urls.py that referenced views.py, then run makemigrations. Hope this helps.

Make sure that you don't have any class variables in your code that are calling Django manager

For example:

class SomeViewSet(viewsets.ViewSet):
    se = SomeEntity.objects.first()  # fetching some entity on the class level

    def list(self, request):
    # the rest of the code

So, when you try to create/apply migrations, this variable will also try to initialise, and will try to access SomeEntity , but at that moment that entity doesn't even exist, and the error occurs.

如果提到的所有其他解决方案都失败了,如果您仍在开发中,最简单的解决方案可能是删除数据库(在 pgAdmin 4 2.0 中,右键单击数据库)然后运行makemigrationsmigrate

Try to migrate particular app using following process. Refer Django migrations

python manage.py makemigrations

Initial migration created then run migrate command with app name

python manage.py migrate appname1, appname2

If you're running in local, For each Django app (maybe you have only one), erase the content of the migrations folder. Then, run python manage.py makemigrations app1 app2 app3 (if you have 3 Django apps named app1, app2, app3). This will (re)create the migrations files required to migrate your database

Then, run python manage.py migrate . It will apply the migration files you just created.

This error may have related to previous database error.so if you created new database and you also face that type of error ,you can simply run the command with the app name:

1)python manage.py makemigrations <"app name">
2)python manage.py migrate <"app name">

I've solved this error with this solution.

first remove all url in urls.py .

create simple function view for viewing nothing.

def simple(request):
    context = {}
    return render(request, 'base.html', context)

and add url to urs.py

do migrate

python manage.py migrate

after migrate, recover the deleted urls.py contents

:)

For me the error came from some initialization code I put into the app.ready() method. Commenting that part of code allowed me to run the command makemigrations without any issue.

I believe app.ready is called at some point by manage.py even for the makemigrations command, which caused my code to query my database before any migration.

I found the problematic code thanks to the traceback.

Your app is trying to call some DB entries that does not exist. If you are trying to migrate it to a new database, one of your options is to export a dump of old database and import it to your new DB .

For example in PostgreSQL, import the database using below command then migration will work!

sudo -u postgres -i psql mydb < mydb-export.sql

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM