--- /dev/null
+.. change::
+ :tags: bug, postgresql
+ :tickets: 4463
+
+ 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.
+
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
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):
from sqlalchemy.engine import reflection
from sqlalchemy.sql.schema import CheckConstraint
from sqlalchemy.testing import fixtures
+from sqlalchemy.testing import mock
from sqlalchemy.testing.assertions import assert_raises
from sqlalchemy.testing.assertions import AssertsExecutionResults
from sqlalchemy.testing.assertions import eq_
},
)
+ def test_reflect_check_warning(self):
+ conn = mock.Mock(
+ execute=lambda *arg, **kw: mock.Mock(
+ fetchall=lambda: [("some name", "NOTCHECK foobar")]
+ )
+ )
+ with mock.patch.object(
+ testing.db.dialect, "get_table_oid", lambda *arg, **kw: 1
+ ):
+ with testing.expect_warnings(
+ "Could not parse CHECK constraint text: 'NOTCHECK foobar'"
+ ):
+ testing.db.dialect.get_check_constraints(conn, "foo")
+
class CustomTypeReflectionTest(fixtures.TestBase):
class CustomType(object):