Обсуждение: changing varchar to int

Поиск
Список
Период
Сортировка

changing varchar to int

От
Ankur Kaushik
Дата:
Original column



Column                   Type                                 Modifiers
model                | character varying(50)       | default NULL::character varying


Required Column

Model                   integer


=========
ALTER TABLE device ALTER COLUMN model TYPE integer USING (model::integer);


getting below error

ERROR:  default for column "model" cannot be cast automatically to type integer

Re: changing varchar to int

От
Ian Barwick
Дата:
On 15/07/23 16:27, Ankur Kaushik wrote:
> Original column
>
>
>
> Column                   Type                                 Modifiers
> model                | character varying(50)       | default NULL::character varying
>
>
> Required Column
>
> Model                   integer
>
>
> =========
> ALTER TABLE device ALTER COLUMN model TYPE integer USING (model::integer);
>
>
> getting below error
>
> ERROR:  default for column "model" cannot be cast automatically to type integer

You'll need to drop the column default and re-add it after converting the
column.


Regards

Ian Barwick

--
 Ian Barwick                   http://www.2ndQuadrant.com/
 PostgreSQL Development, 24x7 Support, RemoteDBA, Training & Services


Re: changing varchar to int

От
Matheus de Oliveira
Дата:

On Thu, Jul 23, 2015 at 6:41 AM, Ian Barwick <ian@2ndquadrant.com> wrote:
> model                | character varying(50)       | default NULL::character varying
>
>
> Required Column
>
> Model                   integer
>
>
> =========
> ALTER TABLE device ALTER COLUMN model TYPE integer USING (model::integer);
>
>
> getting below error
>
> ERROR:  default for column "model" cannot be cast automatically to type integer

You'll need to drop the column default and re-add it after converting the
column.

You are correct, but in this specific case I don't see much point of having a DEFAULT NULL anyway (NULL is already the default, when there is no DEFAULT [confusing? xD ]). I'd just drop the DEFAULT and then change the type:

    ALTER TABLE device
        ALTER model DROP DEFAULT,

       
ALTER model TYPE integer USING (model::integer);


Regards,
--
Matheus de Oliveira