Using Django to Design Your Database Schema, page 4
Posted Aug. 13, 2008 at 8:57 a.m. by wheaties
Use Django's Object-Relational Mapper to design your database quickly and easily--regardless of the programming language/framework used in your final product.
Using This SQL
There are several ways you can use the SQL generated by Django. If you want to make your life really easy, you can have Django create the tables for you directly. Assuming that you have specified all of the appropriate database information in your settings.py file, you can simply run the following command:
./manage.py syncdb
This will execute the queries generated earlier directly on your database, creating the tables (if they don't already exist). Please note that this command currently will not update your schema if the table exists but is missing a column or two. You must either do that manually or drop the table in question and then execute the syncdb command.
Another option, if you want to keep your DDL(Data Definition Language) in a separate script (maybe if you want to keep it in some sort of version control) is something like:
./manage.py sqlall specialapp > specialapp-ddl-080813.sql
This just puts the output of the sqlall command into a file called specialapp-ddl-080813.sql for later use.
Benefits of Using Django To Create Your Schema
- Simple: I personally find the syntax of Django models to be very simple and direct. There is a comprehensive API that explains and demonstrates what Django models are capable of.
- Fast: Being that the syntax is so simple, I find that it makes designing and defining your schema much faster than trying to do it with raw SQL or using a database administration GUI.
- Understandable: Looking at the model code in Django is not nearly as intimidating as similar solutions in other frameworks (think about Java Persistence API models).
- Intelligent: Using the same model code, Django can generate proper Data Definition Language SQL for several popular database servers. It handles indexes, keys, relationships, transactions, etc. and can tell the difference between server types.
Downfalls of Using Django To Create Your Schema
- The Table Prefix: Notice how all of the tables in the SQL above were prefixed with specialapp_. That's Django's safe way of making sure models from different applications in the same Django project do not interfere with each other. However, if you don't plan on using Django for your end project, the prefix could be a major annoyance. There are a couple solutions:
- A simple "search and replace" before executing the SQL in your database
- Define the db_table option in your models
- Another Technology: Django (or even Python) may or may not be in your organization's current development stack. If it's not, using the methods described in this article would just become one more thing to support.
Other Thoughts
I first thought about doing the things mentioned in this application when I was working on a personal Java application. I like to use JPA when developing database-backed applications in Java because it abstracts away a lot of the database operations. However, I don't like coming up with the model classes directly, so I usually reverse engineer them from existing database tables.
Before thinking about the things discussed in this article, I created the tables by hand, making several modifications to the schema before I was satisfied with my JPA models. This proved to be quite bothersome and time-consuming.
After using Django to develop my tables, the JPA models turned out to be a lot more reliable, and they were usually designed properly from the get-go. I haven't created tables manually ever since.
If you find yourself designing database schemas often, and you find that you have to make several changes to your tables before you/the project requirements are satisfied, you might consider using Django to do the grunt work. It's worked for me, and I'm sure it will work for you too.
Good luck!
Comments
Post a Comment
This article is closed for comments.

Django is the solution to so many problems, isn't it. Hehe, even for created Java apps using JPA. :) Haha, I love that.
Great pro/con discussion at the end of the article.
Hey...that is a pretty good idea. I am one of those people who don't currently have Django in the work place. That is something I will have to try to get around. :) Great article.
Once again the Jedi Master Ninja Super Programmer has come through with an amazing article that not only offers great solutions, but in an extremely efficient and clean way. Maybe someday I'll understand all of this a fraction as well as he does.
lol.
Nice article.
In addition, if you install the django-command-extensions app, you can use Django to create entity relationship diagrams for your database (using the graph_models subcommand), even if you don't end up using Django for the end product. I did that as part of the planning phase for a project and it turned out really nicely.
@Jason: Thanks for that info! I'll definitely have to check that out!