Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how about on update? #643

Closed
RekGRpth opened this issue Oct 7, 2020 · 1 comment
Closed

how about on update? #643

RekGRpth opened this issue Oct 7, 2020 · 1 comment

Comments

@RekGRpth
Copy link

RekGRpth commented Oct 7, 2020

diff --git a/pydal/dialects/base.py b/pydal/dialects/base.py
index 4b3bf590..ea3977e2 100644
--- a/pydal/dialects/base.py
+++ b/pydal/dialects/base.py
@@ -109,7 +109,7 @@ class SQLDialect(CommonDialect):
     def type_reference(self):
         return (
             "INTEGER REFERENCES %(foreign_key)s "
-            + "ON DELETE %(on_delete_action)s %(null)s %(unique)s"
+            + "ON DELETE %(on_delete_action)s ON UPDATE %(on_update_action)s %(null)s %(unique)s"
         )
 
     @sqltype_for("list:integer")
@@ -137,7 +137,7 @@ class SQLDialect(CommonDialect):
         return (
             ', CONSTRAINT  "FK_%(constraint_name)s" FOREIGN KEY '
             + "(%(field_name)s) REFERENCES %(foreign_key)s "
-            + "ON DELETE %(on_delete_action)s"
+            + "ON DELETE %(on_delete_action)s ON UPDATE %(on_update_action)s"
         )
 
     def alias(self, original, new):
diff --git a/pydal/dialects/postgre.py b/pydal/dialects/postgre.py
index f5e54cd5..6cb1c43b 100644
--- a/pydal/dialects/postgre.py
+++ b/pydal/dialects/postgre.py
@@ -35,7 +35,7 @@ class PostgreDialect(SQLDialect):
     def type_big_reference(self):
         return (
             "BIGINT REFERENCES %(foreign_key)s "
-            + "ON DELETE %(on_delete_action)s %(null)s %(unique)s"
+            + "ON DELETE %(on_delete_action)s ON UPDATE %(on_update_action)s %(null)s %(unique)s"
         )
 
     @sqltype_for("reference TFK")
@@ -43,7 +43,7 @@ class PostgreDialect(SQLDialect):
         return (
             ' CONSTRAINT "FK_%(constraint_name)s_PK" FOREIGN KEY '
             + "(%(field_name)s) REFERENCES %(foreign_table)s"
-            + "(%(foreign_key)s) ON DELETE %(on_delete_action)s"
+            + "(%(foreign_key)s) ON DELETE %(on_delete_action)s ON UPDATE %(on_update_action)s"
         )
 
     @sqltype_for("geometry")
diff --git a/pydal/migrator.py b/pydal/migrator.py
index 10d4ada5..ce264ac6 100644
--- a/pydal/migrator.py
+++ b/pydal/migrator.py
@@ -119,6 +119,7 @@ class Migrator(object):
                             table_name=table._rname,
                             field_name=field._rname,
                             on_delete_action=field.ondelete,
+                            on_update_action=field.onupdate,
                         )
                 else:
                     # make a guess here for circular references
@@ -149,6 +150,7 @@ class Migrator(object):
                         constraint_name=self.dialect.quote(constraint_name),
                         foreign_key="%s (%s)" % (real_referenced, rfield_rname),
                         on_delete_action=field.ondelete,
+                        on_update_action=field.onupdate,
                     )
                     ftype_info["null"] = (
                         " NOT NULL" if field.notnull else self.dialect.allow_null
@@ -258,11 +260,17 @@ class Migrator(object):
                 table._raw_rname, "_".join(f._raw_rname for f in fk_fields)
             )
             on_delete = list(set(f.ondelete for f in fk_fields))
+            on_update = list(set(f.onupdate for f in fk_fields))
             if len(on_delete) > 1:
                 raise SyntaxError(
                     "Table %s has incompatible ON DELETE actions in multi-field foreign key."
                     % table._dalname
                 )
+            if len(on_update) > 1:
+                raise SyntaxError(
+                    "Table %s has incompatible ON UPDATE actions in multi-field foreign key."
+                    % table._dalname
+                )
             tfk_field_name = ", ".join(fkeys)
             tfk_foreign_key = ", ".join(pkeys)
             tfk_foreign_table = rtable._rname
@@ -283,6 +291,7 @@ class Migrator(object):
                     foreign_table=tfk_foreign_table,
                     foreign_key=tfk_foreign_key,
                     on_delete_action=on_delete[0],
+                    on_update_action=on_update[0],
                 )
             )
 
diff --git a/pydal/objects.py b/pydal/objects.py
index d1371ca9..e7a1b618 100644
--- a/pydal/objects.py
+++ b/pydal/objects.py
@@ -1885,6 +1885,7 @@ class Field(Expression, Serializable):
         required=False,
         requires=DEFAULT,
         ondelete="CASCADE",
+        onupdate="CASCADE",
         notnull=False,
         unique=False,
         uploadfield=True,
@@ -1949,6 +1950,7 @@ class Field(Expression, Serializable):
         self.default = default if default is not DEFAULT else (update or None)
         self.required = required  # is this field required
         self.ondelete = ondelete.upper()  # this is for reference fields only
+        self.onupdate = onupdate.upper()  # this is for reference fields only
         self.notnull = notnull
         self.unique = unique
         # split to deal with decimal(,)
@@ -2198,6 +2200,7 @@ class Field(Expression, Serializable):
             "authorize",
             "represent",
             "ondelete",
+            "onupdate",
             "custom_store",
             "autodelete",
             "custom_retrieve",
@mdipierro
Copy link
Contributor

Thank you. I applied your patch. Sorry it took me so long but I overlooked your patch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants