@@ -501,15 +501,15 @@ def get_value_or_en_translation(cr, table, column):
501501
502502
503503def _column_info (cr , table , column ):
504+ # -> tuple[str, int | None, bool, bool] | None
504505 _validate_table (table )
505- # NOTE: usage of both `CONCAT` and `||` in the query below is done on purpose to take advantage of their NULL handling.
506- # NULLS propagate with `||` and are ignored by `CONCAT`.
507506 cr .execute (
508507 """
509- SELECT CONCAT(
510- COALESCE(bt.typname, t.typname),
511- '(' || information_schema._pg_char_max_length(information_schema._pg_truetypid(a.*, t.*), information_schema._pg_truetypmod(a.*, t.*)) || ')'
512- ) AS udt_name,
508+ SELECT COALESCE(bt.typname, t.typname) AS udt_name,
509+ information_schema._pg_char_max_length(
510+ information_schema._pg_truetypid(a.*, t.*),
511+ information_schema._pg_truetypmod(a.*, t.*)
512+ ) AS char_max_length,
513513 NOT (a.attnotnull OR t.typtype = 'd' AND t.typnotnull) AS is_nullable,
514514 ( c.relkind IN ('r','p','v','f')
515515 AND pg_column_is_updatable(c.oid::regclass, a.attnum, false)
@@ -589,7 +589,7 @@ def column_exists(cr, table, column):
589589 return _COLUMNS [(table , column )] if (table , column ) in _COLUMNS else (_column_info (cr , table , column ) is not None )
590590
591591
592- def column_type (cr , table , column ):
592+ def column_type (cr , table , column , sized = False ):
593593 """
594594 Return the type of a column, if it exists.
595595
@@ -598,17 +598,21 @@ def column_type(cr, table, column):
598598 :rtype: SQL type of the column
599599 """
600600 nfo = _column_info (cr , table , column )
601+ if not nfo :
602+ return None
603+ if sized and nfo [1 ]:
604+ return "{}({})" .format (nfo [0 ], nfo [1 ])
601605 return nfo [0 ] if nfo else None
602606
603607
604608def column_nullable (cr , table , column ):
605609 nfo = _column_info (cr , table , column )
606- return nfo and nfo [1 ]
610+ return nfo and nfo [2 ]
607611
608612
609613def column_updatable (cr , table , column ):
610614 nfo = _column_info (cr , table , column )
611- return nfo and nfo [2 ]
615+ return nfo and nfo [3 ]
612616
613617
614618def _normalize_pg_type (type_ ):
@@ -672,7 +676,7 @@ def create_column(cr, table, column, definition, **kwargs):
672676 if definition == "bool" and default is no_def :
673677 default = False
674678
675- curtype = column_type (cr , table , column )
679+ curtype = column_type (cr , table , column , sized = True )
676680 if curtype :
677681 if curtype != definition :
678682 _logger .error ("%s.%s already exists but is %r instead of %r" , table , column , curtype , definition )
@@ -738,7 +742,7 @@ def alter_column_type(cr, table, column, type, using=None, where=None, logger=_l
738742 raise ValueError ("`where` parameter is only relevant with a non-default `using` parameter" )
739743
740744 if not using :
741- current_type = column_type (cr , table , column )
745+ current_type = column_type (cr , table , column , sized = True )
742746 if current_type and current_type == _normalize_pg_type (type ):
743747 logger .info ("Column %r of table %r is already defined as %r" , column , table , type )
744748 return
@@ -1884,7 +1888,7 @@ def bulk_update_table(cr, table, columns, mapping, key_col="id"):
18841888 cols = ColumnList .from_unquoted (cr , columns ),
18851889 cols_values = SQLStr (
18861890 ", " .join (
1887- "(m.value->>{:d})::{}" .format (col_idx , column_type (cr , table , col_name ))
1891+ "(m.value->>{:d})::{}" .format (col_idx , column_type (cr , table , col_name , sized = True ))
18881892 for col_idx , col_name in enumerate (columns )
18891893 )
18901894 ),
0 commit comments