Added support for reflection of PostgreSQL CHECK constraints marked with
"NO INHERIT", setting the key ``no_inherit=True`` in the reflected data.
Pull request courtesy Ellis Valentiner.
Fixes: #10777
Closes: #10778
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/10778
Pull-request-sha:
058082ff6297f9ccdc4977e65ef024e9a093426e
Change-Id: Ia33e29c0c57cf0076e8819311f4628d712fdc332
--- /dev/null
+.. change::
+ :tags: usecase, postgresql, reflection
+ :tickets: 10777
+
+ Added support for reflection of PostgreSQL CHECK constraints marked with
+ "NO INHERIT", setting the key ``no_inherit=True`` in the reflected data.
+ Pull request courtesy Ellis Valentiner.
# "CHECK (((a > 1) AND (a < 5))) NOT VALID"
# "CHECK (some_boolean_function(a))"
# "CHECK (((a\n < 1)\n OR\n (a\n >= 5))\n)"
+ # "CHECK (a NOT NULL) NO INHERIT"
+ # "CHECK (a NOT NULL) NO INHERIT NOT VALID"
m = re.match(
- r"^CHECK *\((.+)\)( NOT VALID)?$", src, flags=re.DOTALL
+ r"^CHECK *\((.+)\)( NO INHERIT)?( NOT VALID)?$",
+ src,
+ flags=re.DOTALL,
)
if not m:
util.warn("Could not parse CHECK constraint text: %r" % src)
"sqltext": sqltext,
"comment": comment,
}
- if m and m.group(2):
- entry["dialect_options"] = {"not_valid": True}
+ if m:
+ do = {}
+ if " NOT VALID" in m.groups():
+ do["not_valid"] = True
+ if " NO INHERIT" in m.groups():
+ do["no_inherit"] = True
+ if do:
+ entry["dialect_options"] = do
check_constraints[(schema, table_name)].append(entry)
return check_constraints.items()
],
)
+ def test_reflect_with_no_inherit_check_constraint(self):
+ rows = [
+ ("foo", "some name", "CHECK ((a IS NOT NULL)) NO INHERIT", None),
+ (
+ "foo",
+ "some name",
+ "CHECK ((a IS NOT NULL)) NO INHERIT NOT VALID",
+ None,
+ ),
+ ]
+ conn = mock.Mock(
+ execute=lambda *arg, **kw: mock.MagicMock(
+ fetchall=lambda: rows, __iter__=lambda self: iter(rows)
+ )
+ )
+ check_constraints = testing.db.dialect.get_check_constraints(
+ conn, "foo"
+ )
+ eq_(
+ check_constraints,
+ [
+ {
+ "name": "some name",
+ "sqltext": "a IS NOT NULL",
+ "dialect_options": {"no_inherit": True},
+ "comment": None,
+ },
+ {
+ "name": "some name",
+ "sqltext": "a IS NOT NULL",
+ "dialect_options": {"not_valid": True, "no_inherit": True},
+ "comment": None,
+ },
+ ],
+ )
+
def _apply_stm(self, connection, use_map):
if use_map:
return connection.execution_options(