Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I'm studying SQL using PostgreSQL, but now I have a problem that I can't resolve. I have a type that I call Phones:

CREATE TYPE Phone as 
(
  DDD varchar(3),
  Number Varchar(10),
  TypeOf Varchar(15)
)

And i have to create a Array of Phones into the table Students:

CREATE TABLE Students
(
   Id INT NOT NULL,
   PRIMARY KEY (Id),
   name Varchar(50),
   status Char,
   codCourse int,
    FOREIGN KEY (codCourse )  REFERENCES Course (IdCourse) ON DELETE CASCADE,
   phones Phone[] 
)

And I have created. But, when i try insert into Students table like that:

INSERT INTO Students (Id, name, status, codCourse , phones) 
VALUES (
    00001, 'Joaquim Soares Fernando', 'I', 4,       
    Array[('22','33548795','Telefone')])

I got this error message:

ERROR: column "phones" is of type phone[] but expression is of type record[]
SQL state: 42804
Hint: You will need to rewrite or cast the expression.
Character: 118"

How can i do that insert?

share|improve this question

1 Answer 1

You have to explicitly cast an array to the type phone[]:

INSERT INTO students (id, name, status, codcourse , phones) 
VALUES (
    00001, 'Joaquim Soares Fernando', 'I', 4, 
    array[('22','33548795','Telefone')]::phone[]);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.