check database interity

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from django.db import models
from django.conf import settings
from django.db import connection



cursor = connection.cursor()
    

for app in models.get_apps():
    for model in models.get_models(app):
        for f in model._meta.local_fields:
            if(f.rel):
                sql=u"""SELECT `%(table)s`.`%(pk)s` FROM `%(table)s` LEFT JOIN `%(fk_table)s` as fkt ON fkt.`%(fk_pk)s` = `%(table)s`.`%(attname)s` WHERE not isnull(`%(table)s`.`%(attname)s`) AND isnull(fkt.`%(fk_pk)s`);""" % { 'pk': model._meta.pk.attname,
            'table': model._meta.db_table,
            'fk_table' : f.rel.to._meta.db_table,
            'fk_pk' : f.rel.to._meta.pk.attname,
            'attname': f.get_attname()
            }
                cursor.execute(sql)
                errors = len(cursor.fetchall())
                if errors:
                    print "model %s, relation %s (%s) : %s" % (model._meta.app_label+'.'+model.__name__,f.rel.to._meta.db_table, f.get_attname(), errors)
            

More like this

  1. Check condition in Form Queryset by felipecruz 1 year, 5 months ago
  2. Get email and push on couchdb - utils by magik_cypress 1 year, 2 months ago
  3. dumpdata/loaddata with MySQL and ForeignKeys (Revision 2) by cmgreen 5 years, 1 month ago
  4. MySQL "Text" Type Model Field by blackbrrr 4 years, 11 months ago
  5. Django Dictionary Model by Morgul 1 year, 11 months ago

Comments

(Forgotten your password?)