# Inspectdb bug?

**URL:** <https://forum.djangoproject.com/t/inspectdb-bug/19605>\
**Category:** Getting Started\
**Created:** [March 18, 2023, 9:52am UTC](https://forum.djangoproject.com/t/inspectdb-bug/19605 "2023-03-18T09:52:41Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![brun](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/brun/32/12771_2.png) [@brun](https://forum.djangoproject.com/u/brun)\
**Post date:** [March 18, 2023, 9:52am UTC](https://forum.djangoproject.com/t/inspectdb-bug/19605/1 "2023-03-18T09:52:41Z")

</div>

i think there is some bug in inspectdb command. now when i manually use in terminal  
python manage.py inspectdb \> models.py  
it generates ONLY models where name starts with “Auth” and “Django”.  
when i use  
python manage.py inspectdb --database=“data” \> models.py  
it creates models ONLY for that database.  
but previously it generated models for both database and models that are part of Django’s built-in authentication and content types framework in a single models.py.  
could anybody please explain what is happening and how do i fix it?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 18, 2023, 11:03am UTC](https://forum.djangoproject.com/t/inspectdb-bug/19605/2 "2023-03-18T11:03:31Z")

</div>

You’d have to provide a lot more details about your environment for us to even begin to try and help you.

This includes starting with fully describing your environment - What operating system are you using? What database? What version of that database? What versions of Python and Django?

Show the DATABASES section of your settings.py file.

Show some type of output from your database native client (such as psql for a PostgreSQL database) to identify what tables you’re expecting to see managed.

Show that the account you’re using for that database has all the permissions needed to inspect the database.

Now, regarding:

> [@brun](#):
>
> it creates models ONLY for that database.

Yes, which means it will only create the models for the tables in that database. If that database doesn’t have those tables, they aren’t there to be inspected.

---

<div class="post-metadata">

**Author:** ![brun](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/brun/32/12771_2.png) [@brun](https://forum.djangoproject.com/u/brun)\
**Post date:** [March 18, 2023, 11:32am UTC](https://forum.djangoproject.com/t/inspectdb-bug/19605/3 "2023-03-18T11:32:06Z")

</div>

Sorry, first time having such issue that requires posting. Thank you for pointing out what is required for post.  
I use Windows 10, MySQL, Python 3.9.1 and Django 4.1.7.  
DATABASES section of my settings.py:

```auto
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases

import json
# Load the contents of the configuration file
with open("config.json", 'r') as f:
    config = json.load(f)
# Merge the default database configuration with the configuration from config.json
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}
DATABASES.update(config.get('DATABASES', {}))

```

I am expecting to see in models.py all my tables from databases and the classes that are required for app (all those DjangoSession, DjangoMigrations, DjangoContentType, DjangoAdminLog, AuthUserUserPermissions, AuthUserGroups, AuthUser, AuthPermission, AuthGroupPermissions, AuthGroup). It was alright yesterday but today it is generated on ‘either or’ basis (either Django/Auth part or my tables).  
My account has all the permissions.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 18, 2023, 11:56am UTC](https://forum.djangoproject.com/t/inspectdb-bug/19605/4 "2023-03-18T11:56:13Z")

</div>

Side note: When posting code here, enclose the code between lines of three backtick - ` characters. That means you will have a line of ```, then your code, then another line of ```. (When posting code from multiple files, it’s generally most useful to do this for each file individually.) This forces the forum software to keep your code properly formatted. This is also the recommended process when posting templates, error messages, and other pre-formatted text.

You don’t need to repost your code above, you can edit your reply to add the ``` before and after your settings file.

Regarding the DATABASE configuration, you should post what is in your config.json file if that’s overriding what’s defined in your settings.py file.

Also, please post the rest of the information requested.

---

<div class="post-metadata">

**Author:** ![brun](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/brun/32/12771_2.png) [@brun](https://forum.djangoproject.com/u/brun)\
**Post date:** [March 18, 2023, 12:34pm UTC](https://forum.djangoproject.com/t/inspectdb-bug/19605/6 "2023-03-18T12:34:40Z")

</div>

Somehow my first reply disappeared and the second one became incomplete.  
operating system - Windows 10  
database - MySQL 8.0.32  
Python 3.9.1  
Django 4.1.7

DATABASES section of my settings.py file:

```auto
# Database
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases

import json

# Load the contents of the configuration file
with open("config.json", 'r') as f:
    config = json.load(f)

# Merge the default database configuration with the configuration from config.json
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}
DATABASES.update(config.get('DATABASES', {}))

```

Content of my config.json (in “…” was actual values):

```auto
{
    "DATABASES": {
        "data": {
            "ENGINE": "django.db.backends.mysql",
            "NAME": "data",
            "USER": "...",
            "PASSWORD": "...",
            "HOST": "...",
            "PORT": "..."
        },
        "new": {
            "ENGINE": "django.db.backends.mysql",
            "NAME": "new",
            "USER": "...",
            "PASSWORD": "...",
            "HOST": "...",
            "PORT": "..."
        },
        "folder": {
            "ENGINE": "django.db.backends.mysql",
            "NAME": "folder",
            "USER": "...",
            "PASSWORD": "...",
            "HOST": "...",
            "PORT": "..."
        }
    }
}

```

What do you mean by “Show some type of output from your database native client (such as psql for a PostgreSQL database) to identify what tables you’re expecting to see managed” - to provide screenshot how MySQL Workbench shows tables?  
I have all the permissions:

```auto
# Grants for user@host
'GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE, CREATE ROLE, DROP ROLE ON *.* TO `user`@`host` WITH GRANT OPTION'
'GRANT APPLICATION_PASSWORD_ADMIN,AUDIT_ABORT_EXEMPT,AUDIT_ADMIN,AUTHENTICATION_POLICY_ADMIN,BACKUP_ADMIN,BINLOG_ADMIN,BINLOG_ENCRYPTION_ADMIN,CLONE_ADMIN,CONNECTION_ADMIN,ENCRYPTION_KEY_ADMIN,FIREWALL_EXEMPT,FLUSH_OPTIMIZER_COSTS,FLUSH_STATUS,FLUSH_TABLES,FLUSH_USER_RESOURCES,GROUP_REPLICATION_ADMIN,GROUP_REPLICATION_STREAM,INNODB_REDO_LOG_ARCHIVE,INNODB_REDO_LOG_ENABLE,PASSWORDLESS_USER_ADMIN,PERSIST_RO_VARIABLES_ADMIN,REPLICATION_APPLIER,REPLICATION_SLAVE_ADMIN,RESOURCE_GROUP_ADMIN,RESOURCE_GROUP_USER,ROLE_ADMIN,SENSITIVE_VARIABLES_OBSERVER,SERVICE_CONNECTION_ADMIN,SESSION_VARIABLES_ADMIN,SET_USER_ID,SHOW_ROUTINE,SYSTEM_USER,SYSTEM_VARIABLES_ADMIN,TABLE_ENCRYPTION_ADMIN,XA_RECOVER_ADMIN ON *.* TO `user`@`host` WITH GRANT OPTION'
'GRANT PROXY ON ``@`` TO `user`@`host` WITH GRANT OPTION'

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 18, 2023, 1:15pm UTC](https://forum.djangoproject.com/t/inspectdb-bug/19605/7 "2023-03-18T13:15:07Z")

</div>

Do a `show tables` for each of those databases using the mysql client.

Then, for your `default` database, you would use the `sqlite3` command to enter the command-line interface for it, and the `.tables` command will show you the tables in that database.

---

<div class="post-metadata">

**Author:** ![brun](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/brun/32/12771_2.png) [@brun](https://forum.djangoproject.com/u/brun)\
**Post date:** [March 18, 2023, 1:26pm UTC](https://forum.djangoproject.com/t/inspectdb-bug/19605/8 "2023-03-18T13:26:11Z")

</div>

```auto
USE `database name`;
SHOW TABLES;

```

command in MySQL Workbench showed all the tables of each database.  
I don’t use `sqlite3` and default database, just left it from standard settings.py file in case it will be needed in the future.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 18, 2023, 1:27pm UTC](https://forum.djangoproject.com/t/inspectdb-bug/19605/9 "2023-03-18T13:27:41Z")

</div>

> [@brun](#):
>
> I don’t use `sqlite3` and default database,

Yes you do. It’s the default database, it’s going to be used.

> [@brun](#):
>
> command in MySQL Workbench showed all the tables of each database.

Please post the specifics here. (Text output, not an image)

---

<div class="post-metadata">

**Author:** ![brun](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/brun/32/12771_2.png) [@brun](https://forum.djangoproject.com/u/brun)\
**Post date:** [March 18, 2023, 1:33pm UTC](https://forum.djangoproject.com/t/inspectdb-bug/19605/10 "2023-03-18T13:33:08Z")

</div>

```auto
Tables_in_data
2020_09_15_geography7 info
2020_12_30_history5 info

Tables_in_new
2020_09_15_1geography7 info
2020_12_30_1history5 info

Tables_in_folder
2020_09_15_2geography7 info
2020_12_30_2history5 info

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 18, 2023, 1:35pm UTC](https://forum.djangoproject.com/t/inspectdb-bug/19605/11 "2023-03-18T13:35:39Z")

</div>

None of those databases have the Django-related tables in them. That’s why inspectdb doesn’t show them. (They’re not there to be shown.)

It appears from your earlier post the only place they exist is in your default database.

So no, there’s no bug. Inspectdb is working exactly as it should.

---

<div class="post-metadata">

**Author:** ![brun](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/brun/32/12771_2.png) [@brun](https://forum.djangoproject.com/u/brun)\
**Post date:** [March 18, 2023, 1:41pm UTC](https://forum.djangoproject.com/t/inspectdb-bug/19605/12 "2023-03-18T13:41:21Z")

</div>

Could you please elaborate about “Django-related tables” in databases?

Yesterday all those tables were successfully added to models.py with DjangoSession , DjangoMigrations , DjangoContentType , DjangoAdminLog , AuthUserUserPermissions , AuthUserGroups , AuthUser , AuthPermission , AuthGroupPermissions , AuthGroup,

today only by themselves.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 18, 2023, 1:44pm UTC](https://forum.djangoproject.com/t/inspectdb-bug/19605/13 "2023-03-18T13:44:10Z")

</div>

> [@brun](#):
>
> Could you please elaborate about “Django-related tables” in databases?

I’m referring to the tables you listed immediately below:

> [@brun](#):
>
> Yesterday all those tables were successfully added to models.py with DjangoSession , DjangoMigrations , DjangoContentType , DjangoAdminLog , AuthUserUserPermissions , AuthUserGroups , AuthUser , AuthPermission , AuthGroupPermissions , AuthGroup,

You haven’t shown what tables are in your db.sqlite3 file, but based upon what you started out with posting, I expect you would find these tables in there.

(I do wonder why you’re interested in getting them in your models.py file. The proper model definitions already exist within Django.)

---

<div class="post-metadata">

**Author:** ![brun](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/brun/32/12771_2.png) [@brun](https://forum.djangoproject.com/u/brun)\
**Post date:** [March 18, 2023, 1:52pm UTC](https://forum.djangoproject.com/t/inspectdb-bug/19605/14 "2023-03-18T13:52:34Z")

</div>

I assumed if they were added into the models.py yesterday, they are essential part of app for proper functioning.  
I didn’t write what is inside db.sqlite3 file because there is no such file in my django directory.

---

<div class="post-metadata">

**Author:** ![brun](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/brun/32/12771_2.png) [@brun](https://forum.djangoproject.com/u/brun)\
**Post date:** [March 18, 2023, 1:58pm UTC](https://forum.djangoproject.com/t/inspectdb-bug/19605/15 "2023-03-18T13:58:32Z")

</div>

it was new app, just now after executing ‘runserver’ sqlite3 file appeared in directory, but it’s empty

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 18, 2023, 2:04pm UTC](https://forum.djangoproject.com/t/inspectdb-bug/19605/16 "2023-03-18T14:04:04Z")

</div>

First, I can’t address what you’re saying you saw yesterday. There are many reasons why what you’re seeing today is different:

- Tables have been dropped
- Database has been dropped and recreated
- You’re accessing a different database
- You’ve redeployed to a different server where a different db.sqlite3 exists.

So let’s take a step back for a moment.

What are you trying to achieve by doing this? What is your root objective here? (Why are you even running inspectdb at this point in time?)

> [@brun](#):
>
> I didn’t write what is inside db.sqlite3 file because there is no such file in my django directory.

Have you done your initial `migrate` command? That will create the file if it does not already exist.  
(It would be in your project directory as defined in your settings.)

In any event, there is no bug as you have thought - everything you’re seeing today is based upon the databases that are being inspected today.

---

<div class="post-metadata">

**Author:** ![brun](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/brun/32/12771_2.png) [@brun](https://forum.djangoproject.com/u/brun)\
**Post date:** [March 18, 2023, 2:20pm UTC](https://forum.djangoproject.com/t/inspectdb-bug/19605/17 "2023-03-18T14:20:16Z")

</div>

No changes to databases were made since then, I am trying to autogenerate models.py and then use these classes in forms.py and views.py.

After some errors in previous app (I am still studying how I should create Django project), I was trying to recreate all steps from scratch and today already on the step of using inspectdb, its behaviour became different.

Now, if I remember correctly, at the end of yesterday, I updated Windows (don’t know if this info explains anything).

Thank you, migrate command created proper sqlite3 file (I didn’t execute it initially because was mistaken to run it after all the changes).

But models.py still is generated only with my own tables as classes.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 18, 2023, 2:40pm UTC](https://forum.djangoproject.com/t/inspectdb-bug/19605/18 "2023-03-18T14:40:05Z")

</div>

> [@brun](#):
>
> But models.py still is generated only with my own tables as classes.

That’s all you need. That’s all that _you_ should be defining within your models.py If you need to access the django-related tables, you should import their definitions from the appropriate sources.

> [@brun](#):
>
> (I am still studying how I should create Django project)

Go back and work your way through the [Official Django Tutorial](https://docs.djangoproject.com/en/4.0/intro/tutorial01/). That gives you everything you need to get started.

You should _only_ be using `inspectdb` to get model definitions for existing tables that aren’t going to be managed by Django. And even at that, you’re likely going to need to make changes to what has been created. The `inspectdb` command is a starting point, not an ending point.
