3838 TypeDecorator ,
3939 UniqueConstraint ,
4040)
41- from sqlalchemy .dialects .postgresql import DOMAIN , JSONB
41+ from sqlalchemy .dialects .postgresql import DOMAIN , JSON , JSONB
4242from sqlalchemy .engine import Connection , Engine
4343from sqlalchemy .exc import CompileError
4444from sqlalchemy .sql .elements import TextClause
@@ -222,7 +222,7 @@ def collect_imports_for_column(self, column: Column[Any]) -> None:
222222
223223 if isinstance (column .type , ARRAY ):
224224 self .add_import (column .type .item_type .__class__ )
225- elif isinstance (column .type , JSONB ):
225+ elif isinstance (column .type , ( JSONB , JSON ) ):
226226 if (
227227 not isinstance (column .type .astext_type , Text )
228228 or column .type .astext_type .length is not None
@@ -499,7 +499,7 @@ def render_column_callable(self, is_table: bool, *args: Any, **kwargs: Any) -> s
499499 else :
500500 return render_callable ("mapped_column" , * args , kwargs = kwargs )
501501
502- def render_column_type (self , coltype : object ) -> str :
502+ def render_column_type (self , coltype : TypeEngine [ Any ] ) -> str :
503503 args = []
504504 kwargs : dict [str , Any ] = {}
505505 sig = inspect .signature (coltype .__class__ .__init__ )
@@ -515,6 +515,17 @@ def render_column_type(self, coltype: object) -> str:
515515 continue
516516
517517 value = getattr (coltype , param .name , missing )
518+
519+ if isinstance (value , (JSONB , JSON )):
520+ # Remove astext_type if it's the default
521+ if (
522+ isinstance (value .astext_type , Text )
523+ and value .astext_type .length is None
524+ ):
525+ value .astext_type = None # type: ignore[assignment]
526+ else :
527+ self .add_import (Text )
528+
518529 default = defaults .get (param .name , missing )
519530 if isinstance (value , TextClause ):
520531 self .add_literal_import ("sqlalchemy" , "text" )
@@ -547,7 +558,7 @@ def render_column_type(self, coltype: object) -> str:
547558 if (value := getattr (coltype , colname )) is not None :
548559 kwargs [colname ] = repr (value )
549560
550- if isinstance (coltype , JSONB ):
561+ if isinstance (coltype , ( JSONB , JSON ) ):
551562 # Remove astext_type if it's the default
552563 if (
553564 isinstance (coltype .astext_type , Text )
@@ -1224,7 +1235,11 @@ def get_type_qualifiers() -> tuple[str, TypeEngine[Any], str]:
12241235 return "" .join (pre ), column_type , "]" * post_size
12251236
12261237 def render_python_type (column_type : TypeEngine [Any ]) -> str :
1227- python_type = column_type .python_type
1238+ if isinstance (column_type , DOMAIN ):
1239+ python_type = column_type .data_type .python_type
1240+ else :
1241+ python_type = column_type .python_type
1242+
12281243 python_type_name = python_type .__name__
12291244 python_type_module = python_type .__module__
12301245 if python_type_module == "builtins" :
0 commit comments