Paweł Wacławiak
Paweł Wacławiak

Reputation: 50

Django inspectdb unable to inspect table - relation doesn't exist error

So, what I'm trying to achieve is to map the tables I have in my postgres database to django models. I've tried using inspectdb, but it keeps throwing errors and I'm out of ideas on how to fix it. Here is the code I ran:

python3 manage.py inspectdb account

And the output it gives me is:

# This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
#   * Rearrange models' order
#   * Make sure each model has one field with primary_key=True
#   * Make sure each ForeignKey and OneToOneField has `on_delete` set to the desired behavior
#   * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table
# Feel free to rename the models, but don't rename db_table values or field names.
from django.db import models
# Unable to inspect table 'account'
# The error was: relation "account" does not exist
LINE 1: SELECT * FROM "account" LIMIT 1

What I think is going on here is that for some reason the inspectdb is wrapping the table name with double quote which is incorrect to psql syntax and this is the immediate cause of the error.

I have checked if the table is accessible for the database user that django uses and when I tried running the same query, just without the quotes on table name it gave me a correct output:

database=# SELECT * FROM my_schema.account LIMIT 1;
 id | username | email | password_hash | confirmed | last_login_time 
----+----------+-------+---------------+-----------+-----------------
(0 rows)

Django database configuration:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'database',
        'USER': 'db_admin',
        'PASSWORD': 'temporary_password',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

Have anyone encountered this problem and solved it? Or maybe I'm using the tool in a wrong way? Any help appreciated!

Upvotes: 0

Views: 2355

Answers (1)

iklinac
iklinac

Reputation: 15738

You have to add your schema to searchpath

    'OPTIONS': {
        'options': '-c search_path=myschema'
    }

Upvotes: 0

Related Questions