Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 2CF9F12DBEF1 for ; Fri, 30 Dec 2011 23:48:08 -0400 (AST) Received: from mail-iy0-f174.google.com ([209.85.210.174]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RgpvR-0002Vu-8E for pgsql-sql@postgresql.org; Sat, 31 Dec 2011 03:48:07 +0000 Received: by iaeh11 with SMTP id h11so26206090iae.19 for ; Fri, 30 Dec 2011 19:47:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=message-id:date:from:reply-to:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type; bh=aF4UeTdAB907bDix8+8mykdbvGepVuUL2uwWRuTmM/Y=; b=HMjRsESM45ypLTZEQymZXoNuG3rGNxb5382REqRCqmkEiPkdZwYmDasBV9Y8BdsOBg b3Df1XuOqtVKUlzS3+WNy17BwmV+Wx4bZMUnPw+blCb5rutAahqJSHggutkBY3GyksyD 7jVm1ss/2DQ8AgqOQKX58ZulA7lD08CnFxO7s= Received: by 10.42.159.195 with SMTP id m3mr44257492icx.33.1325303272251; Fri, 30 Dec 2011 19:47:52 -0800 (PST) Received: from [192.168.168.2] (70-36-204-192.dsl.static.sonic.net. [70.36.204.192]) by mx.google.com with ESMTPS id x18sm134954231ibi.2.2011.12.30.19.47.50 (version=SSLv3 cipher=OTHER); Fri, 30 Dec 2011 19:47:51 -0800 (PST) Message-ID: <4EFE85E9.9020602@gmail.com> Date: Fri, 30 Dec 2011 19:47:53 -0800 From: "John L. Poole" Reply-To: jlpoole56@gmail.com User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20111105 Thunderbird/8.0 MIME-Version: 1.0 To: David Johnston CC: pgsql-sql@postgresql.org Subject: Re: Nested custom types: array - unable to insert [SOLVED] References: In-Reply-To: Content-Type: multipart/mixed; boundary="------------020006030807000408080905" X-Pg-Spam-Score: -2.6 (--) X-Archive-Number: 201112/108 X-Sequence-Number: 36169 This is a multi-part message in MIME format. --------------020006030807000408080905 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit On 12/30/2011 7:31 PM, David Johnston wrote: > On Dec 30, 2011, at 21:53, John Poole wrote: > >> I am trying to create a custom data type for phone numbers where >> I have a primary phone number and then an array of additional >> phone numbers qualified by certain types. >> >> Below is a set of SQL commands I used to set up my custom >> types. I am unable to insert into the array field and wonder >> if I have found a bug, or I'm just missing some basic technique. >> >> Here is the error I receive when trying to insert into the array field: >> >> ERROR: cannot cast type record[] to phonenumber_type >> LINE 2: ...22222,'office'), (3333333,'cell'),(4444444,'eve')]::phonenum... >> >> Can someone provide me an example of how to insert one or more >> records into the secondary array component of the type or provide >> some light on creating a custom type that would hold two fields: >> 1) a single type >> 2) an array of types (this field may be null) >> >> >> Thank you. >> >> John Poole >> >> ================================================= >> Here is the SQL to recreate my attempt: >> -- >> -- Creating a custom data type and inserting/updating example >> -- >> -- create database demo_typestudy1; >> >> create type phoneattribute_type as ENUM >> ('home','office','day','eve','mobile','fax'); >> >> >> create type phonenumber_type as >> (numbers int, >> phone_type phoneattribute_type >> ); >> >> create type contactphone_type as >> (primarynumber phonenumber_type, >> othernumbers phonenumber_type ARRAY >> ); >> >> create table people >> (id integer PRIMARY KEY, >> username text NOT NULL UNIQUE, >> phone contactphone_type >> ); >> >> -- >> -- create a record w/o phone >> -- >> insert into people >> (id, username) >> VALUES >> (1,'bob'); >> -- >> -- add the custom data type: contactphone, just the primary and no other >> -- we'll try adding array values later >> -- >> update people >> set phone = ((1234567899,'home'),NULL) >> where id = 1; >> -- >> -- also more qualified >> -- >> update people >> set phone = ROW(ROW(1234567899,'home'),NULL) >> where id = 1; >> -- >> -- most qualified (with casting) >> -- >> update people >> set phone = CAST(ROW(CAST(ROW(1234567899,'home') as phonenumber_type),NULL) >> as contactphone_type) >> where id = 1; >> -- >> -- view it >> -- >> select phone >> from people where id = 1; >> -- >> -- try adding to the array field >> -- replace the primary and add additional phone numbers >> -- This is where things go awry. >> -- >> update people >> set phone = CAST(ROW(CAST(ROW(1111111,'home') as >> phonenumber_type),ARRAY[(2222222,'office'), >> (3333333,'cell'),(4444444,'eve')]::phonenumber_type) >> as contactphone_type) >> where id = 1; >> >> > ... ,ARRAY[ ... ]::phonenumber_type[] > > You need to cast to an array of the type which is done by adding the trailing brackets to the type. > > Also, your CREATE TYPE syntax (the "othernumbers phonenumber_type ARRAY" part) is something I have not yet seen. Where did you see this? I would have expected it to read "othernumbers phonenumber_type[]"; without the word ARRAY. > > David J. David et al., That works. demo_typestudy1=# update people demo_typestudy1-# set phone = CAST(ROW(CAST(ROW(1111111,'home') as phonenumber_type),ARRAY[(2222222,'office'), (3333333,'mobile'),(4444444,'eve')]::phonenumber_type[]) demo_typestudy1(# as contactphone_type) demo_typestudy1-# where id = 1; UPDATE 1 demo_typestudy1=# To answer your question, I did originally try "othernumbers phonenumber_type[]" but as I kept running into a wall, I figured I had not tried a correct syntax and somewhere found the "ARRAY" syntax as an acceptable form; I figured the more explicit, the better. It may have been from a railroad diagram. The type definition was accepted so I ran with it. Thank you, I spent several hours gated by this issue, so your insight has been a tremendous help. John --------------020006030807000408080905 Content-Type: text/x-vcard; charset=utf-8; name="jlpoole56.vcf" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="jlpoole56.vcf" begin:vcard fn:John L. Poole n:Poole;John email;internet:jlpoole56@gmail.com tel;work:707-812-1323 tel;home:707-812-1323 x-mozilla-html:TRUE version:2.1 end:vcard --------------020006030807000408080905--