﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
23741	[System Check for migrations] Check presence of all foreign keys	notsqrt	notsqrt	"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."	New feature	closed	Core (System checks)	1.7	Normal	wontfix			Accepted	0	0	0	0	0	0
