'Checks each table name in `table_names` for rows with invalid foreign key references. This method is
intended to be used in conjunction with `disable_constraint_checking()` and `enable_constraint_checking()`, to
determine if rows with invalid references were entered while constraint checks were off.
Raises an IntegrityError on the first invalid foreign key reference encountered (if any) and provides
detailed information about the invalid reference in the error message.
Backends can override this method if they can more directly apply constraint checking (e.g. via "SET CONSTRAINTS
ALL IMMEDIATE")'

def check_constraints(self, table_names=None):

    cursor = self.cursor()
    if (table_names is None):
        table_names = self.introspection.table_names(cursor)
    for table_name in table_names:
        primary_key_column_name = self.introspection.get_primary_key_column(cursor, table_name)
        if (not primary_key_column_name):
            continue
        key_columns = self.introspection.get_key_columns(cursor, table_name)
        for (column_name, referenced_table_name, referenced_column_name) in key_columns:
            cursor.execute((u'\n                                                                                SELECT    REFERRING.`%s`,    REFERRING.`%s`    FROM    `%s`    as    REFERRING\n                                                                                LEFT    JOIN    `%s`    as    REFERRED\n                                                                                ON    (REFERRING.`%s`    =    REFERRED.`%s`)\n                                                                                WHERE    REFERRING.`%s`    IS    NOT    NULL    AND    REFERRED.`%s`    IS    NULL' % (primary_key_column_name, column_name, table_name, referenced_table_name, column_name, referenced_column_name, column_name, referenced_column_name)))
            for bad_row in cursor.fetchall():
                raise utils.IntegrityError((u"The    row    in    table    '%s'    with    primary    key    '%s'    has    an    invalid    foreign    key:    %s.%s    contains    a    value    '%s'    that    does    not    have    a    corresponding    value    in    %s.%s." % (table_name, bad_row[0], table_name, column_name, bad_row[1], referenced_table_name, referenced_column_name)))
