Skip to content
Advertisement

Django inspectdb omitted integer primary key

I have a legacy MySQL db, and I am trying to create Django models from it so I can use the legacy tables in my Django application. I ran inspectdb on the MySQL db and it seemed to import most fields correctly, but failed to import every single primary key and/or id field. Is this the expected behavior? I will admit I’m new to Django but I find it odd that Django wouldn’t use a primary key/id in the same way as SQL. The legacy db holds 17 tables, but I’m only showing one since the behavior was the same for all. Thanks for any insight you might have!

Legacy MySQL table “client”

JavaScript

Result of python manage.py inspectdb > models.py

JavaScript

Thanks in advance for any tips you might have. I searched around for an hour or so but couldn’t find anything on this specific issue.

Advertisement

Answer

Django models by default assign an automatic primary key called id to each model. So, your definitions do include the id fields, just hidden from view.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement