Re: changing varchar to int

From: Matheus de Oliveira <matioli(dot)matheus(at)gmail(dot)com>
To: Ian Barwick <ian(at)2ndquadrant(dot)com>
Cc: Ankur Kaushik <ankurkaushik(at)gmail(dot)com>, pgsql-admin <pgsql-admin(at)postgresql(dot)org>
Subject: Re: changing varchar to int
Date: 2015-07-23 12:55:07
Message-ID: CAJghg4Lthay_SvQNdCu6R486_zk-Wq0LFYH3-tp26dOjuH4hew@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

On Thu, Jul 23, 2015 at 6:41 AM, Ian Barwick <ian(at)2ndquadrant(dot)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

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Shreeyansh Dba 2015-07-25 05:51:49 Reg:Autocommit
Previous Message Ian Barwick 2015-07-23 09:41:02 Re: changing varchar to int