Ticket #7556: support_other_schema.diff

File support_other_schema.diff, 5.0 KB (added by brockweaver, 16 years ago)
  • django/core/management/commands/inspectdb.py

     
    1818
    1919        introspection_module = get_introspection_module()
    2020
    21         table2model = lambda table_name: table_name.title().replace('_', '')
     21        # add support for schema being part of class name (think of a table that references another table in a different schema)
     22        table2model = lambda table_name: table_name.title().replace('_', '').replace(' ', '').replace('.','')
    2223
    2324        cursor = connection.cursor()
    2425        yield "# This is an auto-generated Django model module."
     
    8485                        extra_params['max_digits'] = row[4]
    8586                        extra_params['decimal_places'] = row[5]
    8687
    87                     # Add primary_key and unique, if necessary.
    88                     column_name = extra_params.get('db_column', att_name)
    89                     if column_name in indexes:
    90                         if indexes[column_name]['primary_key']:
    91                             extra_params['primary_key'] = True
    92                         elif indexes[column_name]['unique']:
    93                             extra_params['unique'] = True
    94 
    9588                    field_type += '('
    9689
     90                # Add primary_key and unique, if necessary
     91                # Note a field can appear in more than one index or constraint,
     92                # so we do it for both ForeignKey fields and others
     93                column_name = row[0]
     94                if column_name in indexes:
     95                    if indexes[column_name]['primary_key']:
     96                        extra_params['primary_key'] = True
     97                    elif indexes[column_name]['unique']:
     98                        extra_params['unique'] = True
     99
     100
    97101                # Don't output 'id = meta.AutoField(primary_key=True)', because
    98102                # that's assumed if it doesn't exist.
    99103                if att_name == 'id' and field_type == 'AutoField(' and extra_params == {'primary_key': True}:
  • django/db/backends/mysql/base.py

     
    104104    def quote_name(self, name):
    105105        if name.startswith("`") and name.endswith("`"):
    106106            return name # Quoting once is enough.
    107         return "`%s`" % name
     107        # add support for tablenames passed that also have their schema in their name
     108        return "`%s`" % name.replace('.','`.`')
    108109
    109110    def random_function_sql(self):
    110111        return 'RAND()'
  • django/db/backends/mysql/introspection.py

     
    44import re
    55
    66quote_name = DatabaseOperations().quote_name
    7 foreign_key_re = re.compile(r"\sCONSTRAINT `[^`]*` FOREIGN KEY \(`([^`]*)`\) REFERENCES `([^`]*)` \(`([^`]*)`\)")
     7foreign_key_re = re.compile(r"\sCONSTRAINT `[^`]*` FOREIGN KEY \(`([^`]*)`\) REFERENCES (\`([^`]*)`\.)?`([^`]*)` \(`([^`]*)`\)")
    88
    99def get_table_list(cursor):
    1010    "Returns a list of table names in the current database."
     
    3434    try:
    3535        # This should work for MySQL 5.0.
    3636        cursor.execute("""
    37             SELECT column_name, referenced_table_name, referenced_column_name
     37            SELECT column_name, table_schema, referenced_table_schema, referenced_table_name, referenced_column_name
    3838            FROM information_schema.key_column_usage
    3939            WHERE table_name = %s
    4040                AND table_schema = DATABASE()
     
    5353                    break
    5454                pos = match.end()
    5555                constraints.append(match.groups())
    56 
    57     for my_fieldname, other_table, other_field in constraints:
     56    for my_fieldname, my_schema, other_schema, other_table, other_field in constraints:
     57        #add support for references to tables in other schemas
     58        if other_schema != my_schema:
     59            other_table = '%s.%s' % (other_schema, other_table)
    5860        other_field_index = _name_to_index(cursor, other_table)[other_field]
    5961        my_field_index = my_field_dict[my_fieldname]
    6062        relations[my_field_index] = (other_field_index, other_table)
     
    7173    cursor.execute("SHOW INDEX FROM %s" % quote_name(table_name))
    7274    indexes = {}
    7375    for row in cursor.fetchall():
    74         indexes[row[4]] = {'primary_key': (row[2] == 'PRIMARY'), 'unique': not bool(row[1])}
     76        # if a field appears in multiple indexes (think intersection or union tables), use the first one (primary key essentially)
     77        if not indexes.has_key(row[4]):
     78            indexes[row[4]] = {'primary_key': (row[2] == 'PRIMARY'), 'unique': not bool(row[1])}
    7579    return indexes
    7680
    7781DATA_TYPES_REVERSE = {
Back to Top