Ticket #7556: support_other_schema.diff
File support_other_schema.diff, 5.0 KB (added by , 16 years ago) |
---|
-
django/core/management/commands/inspectdb.py
18 18 19 19 introspection_module = get_introspection_module() 20 20 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('.','') 22 23 23 24 cursor = connection.cursor() 24 25 yield "# This is an auto-generated Django model module." … … 84 85 extra_params['max_digits'] = row[4] 85 86 extra_params['decimal_places'] = row[5] 86 87 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'] = True92 elif indexes[column_name]['unique']:93 extra_params['unique'] = True94 95 88 field_type += '(' 96 89 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 97 101 # Don't output 'id = meta.AutoField(primary_key=True)', because 98 102 # that's assumed if it doesn't exist. 99 103 if att_name == 'id' and field_type == 'AutoField(' and extra_params == {'primary_key': True}: -
django/db/backends/mysql/base.py
104 104 def quote_name(self, name): 105 105 if name.startswith("`") and name.endswith("`"): 106 106 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('.','`.`') 108 109 109 110 def random_function_sql(self): 110 111 return 'RAND()' -
django/db/backends/mysql/introspection.py
4 4 import re 5 5 6 6 quote_name = DatabaseOperations().quote_name 7 foreign_key_re = re.compile(r"\sCONSTRAINT `[^`]*` FOREIGN KEY \(`([^`]*)`\) REFERENCES `([^`]*)` \(`([^`]*)`\)")7 foreign_key_re = re.compile(r"\sCONSTRAINT `[^`]*` FOREIGN KEY \(`([^`]*)`\) REFERENCES (\`([^`]*)`\.)?`([^`]*)` \(`([^`]*)`\)") 8 8 9 9 def get_table_list(cursor): 10 10 "Returns a list of table names in the current database." … … 34 34 try: 35 35 # This should work for MySQL 5.0. 36 36 cursor.execute(""" 37 SELECT column_name, referenced_table_name, referenced_column_name37 SELECT column_name, table_schema, referenced_table_schema, referenced_table_name, referenced_column_name 38 38 FROM information_schema.key_column_usage 39 39 WHERE table_name = %s 40 40 AND table_schema = DATABASE() … … 53 53 break 54 54 pos = match.end() 55 55 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) 58 60 other_field_index = _name_to_index(cursor, other_table)[other_field] 59 61 my_field_index = my_field_dict[my_fieldname] 60 62 relations[my_field_index] = (other_field_index, other_table) … … 71 73 cursor.execute("SHOW INDEX FROM %s" % quote_name(table_name)) 72 74 indexes = {} 73 75 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])} 75 79 return indexes 76 80 77 81 DATA_TYPES_REVERSE = {