'Return a dictionary of {field_name: (field_name_other_table, other_table)}
representing all relationships to the given table.'

def get_relations(self, cursor, table_name):

    relations = {}
    cursor.execute('SELECT    sql    FROM    sqlite_master    WHERE    tbl_name    =    %s    AND    type    =    %s', [table_name, 'table'])
    try:
        results = cursor.fetchone()[0].strip()
    except TypeError:
        return relations
    results = results[(results.index('(') + 1):results.rindex(')')]
    for field_desc in results.split(','):
        field_desc = field_desc.strip()
        if field_desc.startswith('UNIQUE'):
            continue
        m = re.search('references    (\\S*)    ?\\(["|]?(.*)["|]?\\)', field_desc, re.I)
        if (not m):
            continue
        (table, column) = [s.strip('"') for s in m.groups()]
        if field_desc.startswith('FOREIGN    KEY'):
            m = re.match('FOREIGN    KEY\\s*\\(([^\\)]*)\\).*', field_desc, re.I)
            field_name = m.groups()[0].strip('"')
        else:
            field_name = field_desc.split()[0].strip('"')
        cursor.execute('SELECT    sql    FROM    sqlite_master    WHERE    tbl_name    =    %s', [table])
        result = cursor.fetchall()[0]
        other_table_results = result[0].strip()
        (li, ri) = (other_table_results.index('('), other_table_results.rindex(')'))
        other_table_results = other_table_results[(li + 1):ri]
        for other_desc in other_table_results.split(','):
            other_desc = other_desc.strip()
            if other_desc.startswith('UNIQUE'):
                continue
            other_name = other_desc.split('    ', 1)[0].strip('"')
            if (other_name == column):
                relations[field_name] = (other_name, table)
                break
    return relations
