Django: using more than one database with inspectdb?
Asked Answered
M

3

22

My settings file's database section looks like this:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'C:/Users/Desktop/test.db'
    },
    'blah':{
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'C:/Users/Desktop/test2.db'
    }
}

When I run the command python manage.py inspectdb > models.py, I only get the model generated for the default database, but not the second one. How could I get both models generated?

Merits answered 2/6, 2016 at 2:57 Comment(0)
P
37

From the documentation:

--database DATABASE

Specifies the database to introspect. Defaults to default.

So you can inspect your second database with:

python manage.py inspectdb --database blah

You cannot inspect both at the same time.

Peaslee answered 2/6, 2016 at 3:0 Comment(2)
When I try this I'm getting an error that Connection 'blah' doesn't exist. Do I need to do something prior to this? Does the directory I'm in matter?Merits
That would happen if the database file didn't exist. Check that 'C:/Users/Desktop/test2.db' actually exists?Peaslee
A
11

You can specify a specific database like this:

python manage.py inspectdb --database=blah > you_app/models.py
Alarise answered 8/2, 2017 at 17:17 Comment(0)
M
3

If you are trying @solarissmoke's answer for Django 2.*:

Don't wrap the database name with quotes, otherwise it will give you a KeyError and a ConnectionDoesnotExist error.

python manage.py inspectdb --database blah
Marva answered 28/12, 2018 at 21:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.