Skip to content

Commit

Permalink
Fixed error with foreign keys against unique or primary key columns
Browse files Browse the repository at this point in the history
  • Loading branch information
mitch committed Apr 9, 2011
1 parent 0ee086b commit 86f53f6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES
@@ -1,5 +1,6 @@
== 0.5.4
* Sub-parts of indices are now correctly added
* Fixes error with foreign keys against unique or primary key columns

== 0.5.3 / 2010-04-19
* Column types are no longer forced uppercase
Expand Down
8 changes: 6 additions & 2 deletions schemaobject/foreignkey.py
Expand Up @@ -31,6 +31,7 @@ def ForeignKeySchemaBuilder(table):
AND T.CONSTRAINT_TYPE = 'FOREIGN KEY'
AND K.CONSTRAINT_SCHEMA='%s'
AND K.TABLE_NAME='%s'
AND K.REFERENCED_TABLE_NAME is not null
"""
constraints = conn.execute(sql % (table.parent.name, table.name))

Expand Down Expand Up @@ -59,13 +60,16 @@ def ForeignKeySchemaBuilder(table):

fkeys[n] = FKItem

# POSITION_IN_UNIQUE_CONSTRAINT may be None
pos = fk['POSITION_IN_UNIQUE_CONSTRAINT'] or 0

#columns for this fk
if fk['COLUMN_NAME'] not in fkeys[n].columns:
fkeys[n].columns.insert(fk['POSITION_IN_UNIQUE_CONSTRAINT'], fk['COLUMN_NAME'])
fkeys[n].columns.insert(pos, fk['COLUMN_NAME'])

#referenced columns for this fk
if fk['REFERENCED_COLUMN_NAME'] not in fkeys[n].referenced_columns:
fkeys[n].referenced_columns.insert(fk['POSITION_IN_UNIQUE_CONSTRAINT'], fk['REFERENCED_COLUMN_NAME'])
fkeys[n].referenced_columns.insert(pos, fk['REFERENCED_COLUMN_NAME'])

return fkeys

Expand Down

0 comments on commit 86f53f6

Please sign in to comment.