Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django fails to model legacy database with inspectdb

I have a currently empty Geodjango app set up - I am connected to my Postgis database in which I have a table named aadf that I am attempting to create a model from. I am using inspectdb to do this.

I get the following error message:

from django.contrib.gis.db import models
# Unable to inspect table 'aadf'
# The error was: sequence index must be integer, not 'slice'
# Unable to inspect table 'auth_group'
# The error was: sequence index must be integer, not 'slice'
# Unable to inspect table 'auth_group_permissions'
# The error was: sequence index must be integer, not 'slice'

** This error message repeats for multiple other tables that Django has created **

The connection to the database evidently seems fine as it is able to pick up the relevant table name. That said, it also seems to be trying to inspect the other tables Django has created in the database such as 'auth_group' and 'auth_group_permissions'.

like image 910
Jamie Avatar asked Apr 05 '19 08:04

Jamie


People also ask

What is Inspectdb?

inspectdb. Introspects the database tables in the database pointed-to by the DATABASE_NAME setting and outputs a Django model module (a models.py file) to standard output. Use this if you have a legacy database with which you'd like to use Django.

What is the Django command to view a database schema of an existing or legacy database?

Django inspectdb command Django provides a utility to auto-generate models from an existing database via inspectdb command. The output file will be saved to your current directory.

How do I use Inspectdb?

Django Inspectdb Refactor will automatically create the required folders and create separate python files for each model. You can install it via pip or to get the latest version clone this repo. Add ``inspectdb_refactor`` to your ``INSTALLED_APPS`` inside settings.py of your project. make models in that particular app.


1 Answers

I got this error and found that it was caused by using psycopg2 version 2.8 (or 2.8.1) -- downgrading to 2.7.7 made it go away. This is on Windows 10 with Django 2.2.

Edit: I see now that there's a fix in the pipeline for Django 2.2 to support psycopg2 2.8.

Edit (5/1/2019): Django 2.2.1 is out today with a fix adding support for psycopg2 2.8.

like image 119
dhobbs Avatar answered Sep 27 '22 19:09

dhobbs