Giacomo Bagnoli avatar Giacomo Bagnoli committed 18c6563

Added optional onupdate and ondelete params to Operations.create_check_constraint

Comments (0)

Files changed (2)

alembic/operations.py

         alembic.op._remove_proxy()
 
     def _foreign_key_constraint(self, name, source, referent, 
-                                    local_cols, remote_cols):
+                                    local_cols, remote_cols,
+                                    onupdate=None, ondelete=None):
         m = schema.MetaData()
         t1 = schema.Table(source, m, 
                 *[schema.Column(n, NULLTYPE) for n in local_cols])
         f = schema.ForeignKeyConstraint(local_cols, 
                                             ["%s.%s" % (referent, n) 
                                             for n in remote_cols],
-                                            name=name
+                                            name=name,
+                                            onupdate=onupdate,
+                                            ondelete=ondelete
                                             )
         t1.append_constraint(f)
 
 
 
     def create_foreign_key(self, name, source, referent, local_cols, 
-                                    remote_cols):
+                           remote_cols, onupdate=None, ondelete=None):
         """Issue a "create foreign key" instruction using the 
         current migration context.
 
          source table.
         :param remote_cols: a list of string column names in the
          remote table.
+        :param onupdate: Optional string. If set, emit ON UPDATE <value> when
+         issuing DDL for this constraint. Typical values include CASCADE,
+         DELETE and RESTRICT.
+        :param ondelete: Optional string. If set, emit ON DELETE <value> when
+         issuing DDL for this constraint. Typical values include CASCADE,
+         DELETE and RESTRICT.
 
         """
 
         self.impl.add_constraint(
                     self._foreign_key_constraint(name, source, referent, 
-                            local_cols, remote_cols)
+                            local_cols, remote_cols,
+                            onupdate=onupdate, ondelete=ondelete)
                 )
 
     def create_unique_constraint(self, name, source, local_cols, **kw):
             "REFERENCES t2 (bat, hoho)"
     )
 
+def test_add_foreign_key_onupdate():
+    context = op_fixture()
+    op.create_foreign_key('fk_test', 't1', 't2',
+                    ['foo', 'bar'], ['bat', 'hoho'],
+                    onupdate='CASCADE')
+    context.assert_(
+        "ALTER TABLE t1 ADD CONSTRAINT fk_test FOREIGN KEY(foo, bar) "
+            "REFERENCES t2 (bat, hoho) ON UPDATE CASCADE"
+    )
+
+def test_add_foreign_key_ondelete():
+    context = op_fixture()
+    op.create_foreign_key('fk_test', 't1', 't2',
+                    ['foo', 'bar'], ['bat', 'hoho'],
+                    ondelete='CASCADE')
+    context.assert_(
+        "ALTER TABLE t1 ADD CONSTRAINT fk_test FOREIGN KEY(foo, bar) "
+            "REFERENCES t2 (bat, hoho) ON DELETE CASCADE"
+    )
+
 def test_add_check_constraint():
     context = op_fixture()
     op.create_check_constraint(
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.