11
11
from sphinx .util .docutils import SphinxDirective
12
12
from sqlalchemy import Column , Constraint , inspect
13
13
from sqlalchemy .orm .mapper import Mapper
14
+ from sqlalchemy .sql .elements import ClauseElement
14
15
from sqlalchemy .sql .schema import (
15
16
CheckConstraint ,
16
17
ForeignKeyConstraint ,
@@ -169,6 +170,14 @@ def add_content(self, mapper: Mapper, definition: nodes.definition) -> None:
169
170
definition [- 1 ] += nodes .list_item ("" , nodes .paragraph (text = text ))
170
171
171
172
173
+ def check_constraint_to_str (constraint : CheckConstraint ) -> str :
174
+ if isinstance (constraint .sqltext , ClauseElement ):
175
+ text = constraint .sqltext .compile (compile_kwargs = {"literal_binds" : True })
176
+ else :
177
+ text = getattr (constraint .sqltext , "text" , "" )
178
+ return f"CHECK ({ text } )"
179
+
180
+
172
181
def contraint_to_str (constraint : Constraint ) -> str :
173
182
"""Convert a constraint to a string."""
174
183
if isinstance (constraint , PrimaryKeyConstraint ):
@@ -182,7 +191,7 @@ def contraint_to_str(constraint: Constraint) -> str:
182
191
if isinstance (constraint , UniqueConstraint ):
183
192
return f"UNIQUE ({ ', ' .join (c .name for c in constraint .columns )} )"
184
193
if isinstance (constraint , CheckConstraint ):
185
- return f"CHECK ( { constraint . sqltext . text } )" # type: ignore
194
+ return check_constraint_to_str ( constraint )
186
195
return "UNKNOWN"
187
196
188
197
0 commit comments