From bc6fbfa84ab6e1e9639e00cc23b3c41ab1d30dc1 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 14 Jul 2006 20:06:09 +0000 Subject: overhaul to schema, addition of ForeignKeyConstraint/ PrimaryKeyConstraint objects (also UniqueConstraint not completed yet). table creation and reflection modified to be more oriented towards these new table-level objects. reflection for sqlite/postgres/mysql supports composite foreign keys; oracle/mssql/firebird not converted yet. --- lib/sqlalchemy/databases/mysql.py | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) (limited to 'lib/sqlalchemy/databases/mysql.py') diff --git a/lib/sqlalchemy/databases/mysql.py b/lib/sqlalchemy/databases/mysql.py index 997010f1c..1d587ff7c 100644 --- a/lib/sqlalchemy/databases/mysql.py +++ b/lib/sqlalchemy/databases/mysql.py @@ -309,8 +309,6 @@ class MySQLDialect(ansisql.ANSIDialect): break #print "row! " + repr(row) if not found_table: - tabletype, foreignkeyD = self.moretableinfo(connection, table=table) - table.kwargs['mysql_engine'] = tabletype found_table = True (name, type, nullable, primary_key, default) = (row[0], row[1], row[2] == 'YES', row[3] == 'PRI', row[4]) @@ -338,16 +336,15 @@ class MySQLDialect(ansisql.ANSIDialect): argslist = re.findall(r'(\d+)', args) coltype = coltype(*[int(a) for a in argslist], **kw) - arglist = [] - fkey = foreignkeyD.get(name) - if fkey is not None: - arglist.append(schema.ForeignKey(fkey)) - - table.append_item(schema.Column(name, coltype, *arglist, + table.append_item(schema.Column(name, coltype, **dict(primary_key=primary_key, nullable=nullable, default=default ))) + + tabletype = self.moretableinfo(connection, table=table) + table.kwargs['mysql_engine'] = tabletype + if not found_table: raise exceptions.NoSuchTableError(table.name) @@ -368,15 +365,15 @@ class MySQLDialect(ansisql.ANSIDialect): match = re.search(r'\b(?:TYPE|ENGINE)=(?P.+)\b', desc[lastparen.start():], re.I) if match: tabletype = match.group('ttype') - foreignkeyD = {} - fkpat = (r'FOREIGN KEY\s*\(`?(?P.+?)`?\)' - r'\s*REFERENCES\s*`?(?P.+?)`?' - r'\s*\(`?(?P.+?)`?\)' - ) + + fkpat = r'CONSTRAINT `(?P.+?)` FOREIGN KEY \((?P.+?)\) REFERENCES `(?P.+?)` \((?P.+?)\)' for match in re.finditer(fkpat, desc): - foreignkeyD[match.group('name')] = match.group('reftable') + '.' + match.group('refcol') + columns = re.findall(r'`(.+?)`', match.group('columns')) + refcols = [match.group('reftable') + "." + x for x in re.findall(r'`(.+?)`', match.group('refcols'))] + constraint = schema.ForeignKeyConstraint(columns, refcols, name=match.group('name')) + table.append_item(constraint) - return (tabletype, foreignkeyD) + return tabletype class MySQLCompiler(ansisql.ANSICompiler): @@ -411,12 +408,8 @@ class MySQLSchemaGenerator(ansisql.ANSISchemaGenerator): if not column.nullable: colspec += " NOT NULL" if column.primary_key: - if not override_pk: - colspec += " PRIMARY KEY" if not column.foreign_key and first_pk and isinstance(column.type, sqltypes.Integer): colspec += " AUTO_INCREMENT" - if column.foreign_key: - colspec += ", FOREIGN KEY (%s) REFERENCES %s(%s)" % (column.name, column.foreign_key.column.table.name, column.foreign_key.column.name) return colspec def post_create_table(self, table): -- cgit v1.2.1