'backport of django\'s introspection.get_constraints(...)'

def get_constraints(self, cursor, table_name, introspection):

    constraints = {}
    cursor.execute("\n                                                SELECT\n                                                                kc.constraint_name,\n                                                                kc.column_name,\n                                                                c.constraint_type,\n                                                                array(SELECT    table_name::text    ||    '.'    ||    column_name::text    FROM    information_schema.constraint_column_usage    WHERE    constraint_name    =    kc.constraint_name)\n                                                FROM    information_schema.key_column_usage    AS    kc\n                                                JOIN    information_schema.table_constraints    AS    c    ON\n                                                                kc.table_schema    =    c.table_schema    AND\n                                                                kc.table_name    =    c.table_name    AND\n                                                                kc.constraint_name    =    c.constraint_name\n                                                WHERE\n                                                                kc.table_schema    =    %s    AND\n                                                                kc.table_name    =    %s\n                                ", ['public', table_name])
    for (constraint, column, kind, used_cols) in cursor.fetchall():
        if (constraint not in constraints):
            constraints[constraint] = {'columns': [], 'primary_key': (kind.lower() == 'primary    key'), 'unique': (kind.lower() in ['primary    key', 'unique']), 'foreign_key': (tuple(used_cols[0].split('.', 1)) if (kind.lower() == 'foreign    key') else None), 'check': False, 'index': False}
        constraints[constraint]['columns'].append(column)
    cursor.execute("\n                                                SELECT    kc.constraint_name,    kc.column_name\n                                                FROM    information_schema.constraint_column_usage    AS    kc\n                                                JOIN    information_schema.table_constraints    AS    c    ON\n                                                                kc.table_schema    =    c.table_schema    AND\n                                                                kc.table_name    =    c.table_name    AND\n                                                                kc.constraint_name    =    c.constraint_name\n                                                WHERE\n                                                                c.constraint_type    =    'CHECK'    AND\n                                                                kc.table_schema    =    %s    AND\n                                                                kc.table_name    =    %s\n                                ", ['public', table_name])
    for (constraint, column) in cursor.fetchall():
        if (constraint not in constraints):
            constraints[constraint] = {'columns': [], 'primary_key': False, 'unique': False, 'foreign_key': None, 'check': True, 'index': False}
        constraints[constraint]['columns'].append(column)
    cursor.execute('\n                                                SELECT\n                                                                c2.relname,\n                                                                ARRAY(\n                                                                                SELECT    (SELECT    attname    FROM    pg_catalog.pg_attribute    WHERE    attnum    =    i    AND    attrelid    =    c.oid)\n                                                                                FROM    unnest(idx.indkey)    i\n                                                                ),\n                                                                idx.indisunique,\n                                                                idx.indisprimary\n                                                FROM    pg_catalog.pg_class    c,    pg_catalog.pg_class    c2,\n                                                                pg_catalog.pg_index    idx\n                                                WHERE    c.oid    =    idx.indrelid\n                                                                AND    idx.indexrelid    =    c2.oid\n                                                                AND    c.relname    =    %s\n                                ', [table_name])
    for (index, columns, unique, primary) in cursor.fetchall():
        if (index not in constraints):
            constraints[index] = {'columns': list(columns), 'primary_key': primary, 'unique': unique, 'foreign_key': None, 'check': False, 'index': True}
    return constraints
