Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make sure Django models match the database schema

Tags:

django

Is there any script out there that check whether a Django model matches the corresponding database table (after syncdb)? I need to do this before I convert to South, and my models are pretty huge, so doing it by manual eyeballing is susceptible to errors. Thanks!

like image 358
kevin_82 Avatar asked Apr 27 '11 15:04

kevin_82


People also ask

What is the Django command to view a database schema of an existing database?

Django inspectdb command Django provides a utility to auto-generate models from an existing database via inspectdb command. The output file will be saved to your current directory.

How is Django DB models model used to define data models?

Django web applications access and manage data through Python objects referred to as models. Models define the structure of stored data, including the field types and possibly also their maximum size, default values, selection list options, help text for documentation, label text for forms, etc.


1 Answers

To get a diff between your model definitions and the actual database fields you could take a look at django-extensions

Especially ./manage.py sqldiff

Django command that scans all models for the given appnames and compares there database schema with the real database tables.

It indicates how columns in the database are different from the SQL that would be generated by Django. This command is not a database migration tool. Though it might certainly be of help during migrations. It’s purpose is to show the current differences as a way to checking or debugging your models compared to the real database tables and columns.

like image 176
arie Avatar answered Sep 18 '22 14:09

arie