#23741: [System Check for migrations] Check presence of all foreign keys
--------------------------------------+--------------------
     Reporter:  notsqrt               |      Owner:  nobody
         Type:  Uncategorized         |     Status:  new
    Component:  Core (System checks)  |    Version:  1.7
     Severity:  Normal                |   Keywords:
 Triage Stage:  Unreviewed            |  Has patch:  0
Easy pickings:  0                     |      UI/UX:  0
--------------------------------------+--------------------
 Hi !

 Just had some troubles when upgrading to django 1.7, which led to some
 discussions on https://groups.google.com/forum/#!topic/django-
 users/x_7Fluj2ty8

 The bottom line is : make sure that any app that depends on apps with
 migrations also uses migrartions.
 This is indeed stated in the docs :
 https://docs.djangoproject.com/en/1.7/topics/migrations/#unmigrated-
 dependencies

 But the consequences are, I think, not sufficiently flagged.

 One of the symptoms is that '''foreign key constraints won't be
 created'''.

 This ticket suggests two things:

  * highlighting more clearly the consequences of not using migrations
 (probably simply by putting that section in a "danger" block)
  * adding a system check that all foreign keys are effectively enforced.

 For the second point, it goes like:


 {{{#!python

 @checks.register()
 def check_db_constraints(*args, **kwargs):

     from django.apps import apps
     from django.db import connection
     from django.db.models.fields.related import RelatedField

     errors = []

     for model in apps.get_models():

         if not model._meta.managed:
             continue

         relations =
 connection.introspection.get_relations(connection.cursor(),
 model._meta.db_table)

         for field_id, field_info in
 enumerate(model._meta.get_fields_with_model()):
             field_model = field_info[0]

             if isinstance(field_model, RelatedField):
                 if field_id not in relations:
                     errors.append(
                         checks.Error(
                             "Field %s of model %s has no detected database
 relation." % (field_model, model.__name__),
                             hint="Check that the app has migrations if it
 depends on apps that use them.",
                         )
                     )

     return errors
 }}}

 It probably needs some refinements for multiple db connections and
 detection of backends that do not support constraints.

--
Ticket URL: <https://code.djangoproject.com/ticket/23741>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.1a7889f69f33f2f478050fb840285028%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to