summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2006-07-22 06:21:58 +0000
committerMike Bayer <mike_mp@zzzcomputing.com>2006-07-22 06:21:58 +0000
commit946984d812dcade87a6cbefbb896e4a17af8170e (patch)
treedf8794e80f557baedac9fbdb5720f12b6e470c2a
parentf65fdce01daec6c49359f04d90669919d836e3b5 (diff)
downloadsqlalchemy-946984d812dcade87a6cbefbb896e4a17af8170e.tar.gz
fixed reflection of foreign keys to autoload the referenced table
if it was not loaded already, affected postgres, mysql, oracle. fixes the latest in [ticket:105]
-rw-r--r--CHANGES2
-rw-r--r--lib/sqlalchemy/databases/information_schema.py2
-rw-r--r--lib/sqlalchemy/databases/mysql.py1
-rw-r--r--lib/sqlalchemy/databases/oracle.py1
-rw-r--r--test/engine/reflection.py10
5 files changed, 12 insertions, 4 deletions
diff --git a/CHANGES b/CHANGES
index 8e03c8eec..a347df98d 100644
--- a/CHANGES
+++ b/CHANGES
@@ -3,6 +3,8 @@
by not raising an error when redundant mappers were set up, fixed
- added allow_null_pks option to Mapper, allows rows where some
primary key columns are null (i.e. when mapping to outer joins etc)
+- fixed reflection of foreign keys to autoload the referenced table
+if it was not loaded already
0.2.6
- big overhaul to schema to allow truly composite primary and foreign
diff --git a/lib/sqlalchemy/databases/information_schema.py b/lib/sqlalchemy/databases/information_schema.py
index 296db2de5..291637e9e 100644
--- a/lib/sqlalchemy/databases/information_schema.py
+++ b/lib/sqlalchemy/databases/information_schema.py
@@ -185,8 +185,10 @@ def reflecttable(connection, table, ischema_names):
if current_schema == referred_schema:
referred_schema = table.schema
if referred_schema is not None:
+ schema.Table(referred_table, table.metadata, autoload=True, schema=referred_schema, autoload_with=connection)
refspec = ".".join([referred_schema, referred_table, referred_column])
else:
+ schema.Table(referred_table, table.metadata, autoload=True, autoload_with=connection)
refspec = ".".join([referred_table, referred_column])
if constrained_column not in fk[0]:
fk[0].append(constrained_column)
diff --git a/lib/sqlalchemy/databases/mysql.py b/lib/sqlalchemy/databases/mysql.py
index 1d587ff7c..3f3e8d148 100644
--- a/lib/sqlalchemy/databases/mysql.py
+++ b/lib/sqlalchemy/databases/mysql.py
@@ -370,6 +370,7 @@ class MySQLDialect(ansisql.ANSIDialect):
for match in re.finditer(fkpat, desc):
columns = re.findall(r'`(.+?)`', match.group('columns'))
refcols = [match.group('reftable') + "." + x for x in re.findall(r'`(.+?)`', match.group('refcols'))]
+ schema.Table(match.group('reftable'), table.metadata, autoload=True, autoload_with=connection)
constraint = schema.ForeignKeyConstraint(columns, refcols, name=match.group('name'))
table.append_item(constraint)
diff --git a/lib/sqlalchemy/databases/oracle.py b/lib/sqlalchemy/databases/oracle.py
index 04950674a..c279da619 100644
--- a/lib/sqlalchemy/databases/oracle.py
+++ b/lib/sqlalchemy/databases/oracle.py
@@ -237,6 +237,7 @@ class OracleDialect(ansisql.ANSIDialect):
fk = ([], [])
fks[cons_name] = fk
refspec = ".".join([remote_table, remote_column])
+ schema.Table(remote_table, table.metadata, autoload=True, autoload_with=connection)
if local_column not in fk[0]:
fk[0].append(local_column)
if refspec not in fk[1]:
diff --git a/test/engine/reflection.py b/test/engine/reflection.py
index 582920b30..7cb31fd77 100644
--- a/test/engine/reflection.py
+++ b/test/engine/reflection.py
@@ -64,12 +64,14 @@ class ReflectionTest(PersistTest):
addresses.create()
# clear out table registry
- users.deregister()
- addresses.deregister()
+ meta.clear()
try:
- users = Table('engine_users', testbase.db, autoload = True)
- addresses = Table('engine_email_addresses', testbase.db, autoload = True)
+ addresses = Table('engine_email_addresses', meta, autoload = True)
+ # reference the addresses foreign key col, which will require users to be
+ # reflected at some point
+ print addresses.c.remote_user_id.foreign_key.column
+ users = Table('engine_users', meta, autoload = True)
finally:
addresses.drop()
users.drop()