在PostgreSQL中包含compsite元素的表中插入时出错

时间:2019-03-02 22:32:26

标签: postgresql

当我尝试插入“酒店”表时出现此错误: 错误:无法将类型记录转换为chambre 第4行:row(array [(250,'2 place',300),(200 ...                                       ^ 详细信息:无法将类型record []转换为第1列中的整数。

create type chambre as(
                   numChambre INTEGER ,
                   typeChambre VARCHAR(30),
                   prix REAL);


create table hotel (
                nom VARCHAR(30),
                Adresse adresse,
                positions positions,
                Chambres chambre[],
                nbPersonnel INTEGER,
                nbEtoile INTEGER,
                telephone VARCHAR(14));         


 insert into hotel values( 'president',
                      row(26,'maraval','oran'),
                      row(27.5,136),
                      row(array[(250,'2 place',300),(200,'1 place',250), 
                      (300,'suite',700)]),
                      60,
                      4,
                      '041-45-86-28');

1 个答案:

答案 0 :(得分:1)

您尚未提供地址的详细信息,但是肯定不需要任何ROW构造函数。

对于Chambres,只需将构造的数组表达式强制转换为chambre[]。最好始终在INSERT语句中明确指定列名,以避免混淆。

INSERT INTO hotel 
            ( 
                nom, 
                adresse, 
                positions, 
                chambres, 
                nbpersonnel, 
                nbetoile, 
                telephone 
            ) 
     VALUES 
     ( 
     'president', 
      (26,'maraval','oran'), 
      (27.5,136), 
      array[(250,'2 place',300),(200,'1 place',250),(300,'suite',700)] :: chambre[], 
      60, 
      4, 
     '041-45-86-28' 
      );

Demo