Django - 将模型代码与数据库进行比较 [英] Django - Compare Model Code to Database

查看:172
本文介绍了Django - 将模型代码与数据库进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我维护一个Django项目,其数据库具有多个与实际数据库不同步的模型约束。因此,例如,一些模型字段具有null = False set,但是数据库允许对应的数据库列有NULL。



我很好奇,如果有一个实用程序,在Django或第三方Python脚本中,它将比较每个表的SHOW CREATE TABLE输出(在这种情况下,使用MySQL语法),并将其与python manage.py sql输出进行比较,以突出差异。



在理想情况下,数据库首先不会与Django模型代码同步,但是由于这是我的位置,

解决方案

./ manage.py inspectdb 生成与数据库中存在的模型对应的模型文件。



使用标准unix diff或任何其他奇特的差异工具找到差异并计划您的迁移策略。



虽然前者似乎更简单和更好,你也可以看到在sql级别的差异。 ./ manage.py sqlall 生成当前db模式的sql,相应地 show create table table-name



您可能需要参考 http://code.google.com/p/django-evolution/ ,它会自动将数据库的状态迁移到当前模型中的状态。 - 但请注意,这个项目是旧的,似乎放弃了。


I maintain a Django project with a database that has several model constraints that have fallen out of sync with the actual database. So, for example, some model fields have null=False set, but the database permits NULLs for the corresponding database column.

I'm curious if there is a utility, either in Django or a third-party Python script, that will compare the SHOW CREATE TABLE output (in this case, using MySQL syntax) for each table and compare it with the python manage.py sql output, to highlight the discrepancies.

Granted, in an ideal situation, the database wouldn't fall out of sync with the Django model code in the first place, but since that's where I am, I'm curious if there's a solution to this problem before I write one myself or do the comparison manually.

解决方案

./manage.py inspectdb generates the model file corresponding to the models that exist within the database.

You can diff it with your current model files using a standard unix diff or any other fancy diffing tool to find the difference and plan your migration strategy.

While the former seems simpler and better, you can also see the diff at the sql level. ./manage.py sqlall generates the sql for the current db schema and correspondingly show create table table-name shows the sql for the table creation.

You might want to refer http://code.google.com/p/django-evolution/ which once auto migrated the state of the db to the one in the current models. - Note however, that this project is old and seems abandoned.

这篇关于Django - 将模型代码与数据库进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆