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?
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:
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.
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:
# 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.
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.
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:
# 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):
{
"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:
# 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'
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.
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.
Yes you do. It’s the default database, it’s going to be used.
Please post the specifics here. (Text output, not an image)
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
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.
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.
I’m referring to the tables you listed immediately below:
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.)
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.
it was new app, just now after executing ‘runserver’ sqlite3 file appeared in directory, but it’s empty
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?)
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.
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.
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.
Go back and work your way through the Official Django Tutorial. 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.