Search code examples
pythondjangodatabasepowerdesigner

use django create database scheme or use powerdesigner create database scheme


I am ready to start a new project with django,and it invloves some operations with database.I have 2 way to get the scheme:

  1. use powerdesigner design the database scheme and output sql,then use python manage.py inspectdb to generate models.
  2. design the model then use python manage.py makemigrations and python manage.py migrate to get the database scheme.

Does someone explain what this 2 ways' difference are and how should I choose?


Solution

  • From inspectdb's documentation:

    Use this if you have a legacy database with which you’d like to use Django. The script will inspect the database and create a model for each table within it.

    Its better if you already have a working DB and want to use django on it. It has limitations like:

    1. If inspectdb cannot map a column’s type to a model field type, it’ll use TextField and will insert the Python comment 'This field type is a guess.' next to the field in the generated model.
    2. If the database column name is a Python reserved word (such as 'pass', 'class' or 'for'), inspectdb will append '_field' to the attribute name. For example, if a table has a column 'for', the generated model will have a field 'for_field', with the db_column attribute set to 'for'. inspectdb will insert the Python comment 'Field renamed because it was a Python reserved word.' next to the field.

    Also its a shortcut method, not recommended for if you are starting django project.

    Also writing models, running makemigration and migrate command will create database scheme containing custom fields such as :

    auth_group                               
    auth_group_permissions                   
    auth_permission                          
    auth_user                                
    auth_user_groups                         
    auth_user_user_permissions      
    

    Which are essential for django system if you want to use its authentication backend, User model etc which are core parts of django.