From 848f1275c74d5e69382c73d81a5daa7e25e36d8e Mon Sep 17 00:00:00 2001 From: Denis Laxalde Date: Thu, 13 Mar 2025 08:13:49 +0100 Subject: [PATCH] Move FK_ON_DELETE as a memoized property on PGDDLCompiler --- lib/sqlalchemy/dialects/postgresql/base.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index ccc426b5bf..4938aba2bd 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -1672,10 +1672,6 @@ RESERVED_WORDS = { "verbose", } -FK_ON_DELETE = re.compile( - r"^(?:RESTRICT|CASCADE|SET (?:NULL|DEFAULT)(?:\s*\(.+\))?|NO ACTION)$", - re.I, -) colspecs = { sqltypes.ARRAY: _array.ARRAY, @@ -2255,9 +2251,17 @@ class PGDDLCompiler(compiler.DDLCompiler): text += self._define_constraint_validity(constraint) return text + @util.memoized_property + def _fk_ondelete_pattern(self): + return re.compile( + r"^(?:RESTRICT|CASCADE|SET (?:NULL|DEFAULT)(?:\s*\(.+\))?" + r"|NO ACTION)$", + re.I, + ) + def define_constraint_ondelete_cascade(self, constraint): return " ON DELETE %s" % self.preparer.validate_sql_phrase( - constraint.ondelete, FK_ON_DELETE + constraint.ondelete, self._fk_ondelete_pattern ) def visit_create_enum_type(self, create, **kw): -- 2.47.3