diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-01-24 16:56:44 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-01-24 19:25:59 -0500 |
commit | 377f12696bb1af17bb14f676a49ef21428d537d3 (patch) | |
tree | 6404ec58d222434282b577b992c88928db3ae9c1 /lib/sqlalchemy/dialects/postgresql/base.py | |
parent | 99b7dd4512364f1f1f11212fd87355ea098a93a6 (diff) | |
download | sqlalchemy-377f12696bb1af17bb14f676a49ef21428d537d3.tar.gz |
Use pg_get_constraintdef for CHECK constraint reflection
Revised the query used when reflecting CHECK constraints to make use of the
``pg_get_constraintdef`` function, as the ``consrc`` column is being
deprecated in PG 12. Thanks to John A Stevenson for the tip.
Fixes: #4463
Change-Id: Ie0ee9bdfddb0635db72b35c2e2e4b27f154162fd
Diffstat (limited to 'lib/sqlalchemy/dialects/postgresql/base.py')
-rw-r--r-- | lib/sqlalchemy/dialects/postgresql/base.py | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index ecdbe0018..e9040fb43 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -3381,7 +3381,7 @@ class PGDialect(default.DefaultDialect): CHECK_SQL = """ SELECT cons.conname as name, - cons.consrc as src + pg_get_constraintdef(cons.oid) as src FROM pg_catalog.pg_constraint cons WHERE @@ -3391,8 +3391,19 @@ class PGDialect(default.DefaultDialect): c = connection.execute(sql.text(CHECK_SQL), table_oid=table_oid) + # samples: + # "CHECK (((a > 1) AND (a < 5)))" + # "CHECK (((a = 1) OR ((a > 2) AND (a < 5))))" + def match_cons(src): + m = re.match(r"^CHECK *\(\((.+)\)\)$", src) + if not m: + util.warn("Could not parse CHECK constraint text: %r" % src) + return "" + return m.group(1) + return [ - {"name": name, "sqltext": src[1:-1]} for name, src in c.fetchall() + {"name": name, "sqltext": match_cons(src)} + for name, src in c.fetchall() ] def _load_enums(self, connection, schema=None): |