]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
#2183: Metadata.reflect() foreign keys include options when the dialect exposes it
authorijl <uijllji@gmail.com>
Sun, 13 Oct 2013 21:13:28 +0000 (17:13 -0400)
committerijl <uijllji@gmail.com>
Sun, 13 Oct 2013 21:13:28 +0000 (17:13 -0400)
lib/sqlalchemy/dialects/mysql/base.py
lib/sqlalchemy/engine/reflection.py
lib/sqlalchemy/sql/schema.py
test/engine/test_reflection.py

index fd6a47a10cb5b99aedab4adb2a519520cb1033c1..f3da25a92d18199730741461691ab7c87f8bb380 100644 (file)
@@ -2175,7 +2175,7 @@ class MySQLDialect(default.DefaultDialect):
             ref_names = spec['foreign']
 
             con_kw = {}
-            for opt in ('name', 'onupdate', 'ondelete'):
+            for opt in ('onupdate', 'ondelete'):
                 if spec.get(opt, False):
                     con_kw[opt] = spec[opt]
 
index a9ccf55397e5c168e6a60d3a3bdfcf4cea79f92f..461f5eb231f7955a741d37d4428a2dbe026b994c 100644 (file)
@@ -564,9 +564,14 @@ class Inspector(object):
                                 )
                 for column in referred_columns:
                     refspec.append(".".join([referred_table, column]))
+            if 'options' in fkey_d:
+                options = fkey_d['options']
+            else:
+                options = {}
             table.append_constraint(
                 sa_schema.ForeignKeyConstraint(constrained_columns, refspec,
-                                               conname, link_to_name=True))
+                                               conname, link_to_name=True,
+                                               **options))
         # Indexes
         indexes = self.get_indexes(table_name, schema)
         for index_d in indexes:
index 92220b0d111ca652418111a48e65b3f9e6564eb9..3f9ff00670fd33fed2dbe8dc81fe55fbf6bea50b 100644 (file)
@@ -2396,7 +2396,9 @@ class ForeignKeyConstraint(Constraint):
                     ondelete=self.ondelete,
                     use_alter=self.use_alter,
                     link_to_name=self.link_to_name,
-                    match=self.match
+                    match=self.match,
+                    deferrable=self.deferrable,
+                    initially=self.initially
                 )
 
         if table is not None:
index 21c0509156801125e84c889cd42e47ad31011a9c..3b23bf31aaefeb5e27b21d336cd61f6df439cb13 100644 (file)
@@ -602,6 +602,47 @@ class ReflectionTest(fixtures.TestBase, ComparesTables):
             is a2.c.user_id
         assert u2.join(a2).onclause.compare(u2.c.id == a2.c.user_id)
 
+    @testing.only_on(['postgresql', 'mysql'])
+    @testing.provide_metadata
+    def test_fk_options(self):
+        """test that foreign key reflection includes options (on
+        backends with {dialect}.get_foreign_keys() support)"""
+
+        # fk whose attributes we're testing on reflection
+        addresses_user_id_fkey = sa.ForeignKey(
+            'users.id',
+            name = 'addresses_user_id_fkey',
+            match='FULL',
+            onupdate='RESTRICT',
+            ondelete='RESTRICT',
+            deferrable=True,
+            initially='DEFERRED'
+        )
+
+        # only test implemented attrs
+        if testing.against('postgresql'):
+            test_attrs = ('match', 'onupdate', 'ondelete', 'deferrable', 'initially')
+        elif testing.against('mysql'):
+            test_attrs = ('onupdate', 'ondelete')
+
+        meta = self.metadata
+        Table('users', meta,
+            Column('id', sa.Integer, primary_key=True),
+            Column('name', sa.String(30)),
+            test_needs_fk=True)
+        Table('addresses', meta,
+            Column('id', sa.Integer, primary_key=True),
+            Column('user_id', sa.Integer, addresses_user_id_fkey),
+            test_needs_fk=True)
+        meta.create_all()
+
+        meta2 = MetaData()
+        meta2.reflect(testing.db)
+        for fk in meta2.tables['addresses'].foreign_keys:
+            ref = locals()[fk.name]
+            for attr in test_attrs:
+                assert getattr(fk, attr) == getattr(ref, attr)
+
     def test_pks_not_uniques(self):
         """test that primary key reflection not tripped up by unique
         indexes"""