Re: Encountered an error

From: Karsten Hilbert <Karsten(dot)Hilbert(at)gmx(dot)net>
To: psycopg(at)postgresql(dot)org
Subject: Re: Encountered an error
Date: 2016-03-10 14:30:14
Message-ID: 20160310143014.GF9358@hermes.hilbert.loc
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

On Thu, Mar 10, 2016 at 05:02:51AM -0800, Adrian Klaver wrote:

>> #First make the appropriate column

> This will not work for the reasons Daniele gave. You have two choices:
>
> 1) Build the SQL string using Python string formatting.
>
> 2) Do it with the Postgres format(). From a psql session:
>
> test=> select format('ALTER tbl_a ADD %I int DEFAULT NULL', 'fld_2');
> format
> ----------------------------------------
> ALTER tbl_a ADD fld_2 int DEFAULT NULL
>
> In psycopg this is a two step process, submit the query and then resubmit
> the returned string.

While this is a nice solution to OPs problem I cannot resist
the urge to point out that to me the whole approach of adding
a column from within client code (outside a database
management application) does have at least a whiff of design
smell to it.

There's probably better ways to implement EAV. Approaches
with JSON, hstore, xml, or the venerable
CREATE TABLE inventory_detail(pk, fk_inventory, detail_name, detail_value)
come to mind.

Regards,
Karsten
--
GPG key ID E4071346 @ eu.pool.sks-keyservers.net
E167 67FD A291 2BEA 73BD 4537 78B9 A9F9 E407 1346

In response to

Responses

Browse psycopg by date

  From Date Subject
Next Message Karsten Hilbert 2016-03-10 14:32:56 Re: Encountered an error
Previous Message Adrian Klaver 2016-03-10 13:02:51 Re: Encountered an error