简体   繁体   English

如何检查TYPE ATTRIBUTE是否已存在

[英]How to check if TYPE ATTRIBUTE already exists

I'm trying to add new attribute to my pg_type and I need to check if there is already type with attribute named 'parentvehicleid'. 我正在尝试将新属性添加到pg_type,并且需要检查是否已经存在名为“ parentvehicleid”的属性。

If I'm adding a column to the table, I can check the column_name as in example: 如果我要向表中添加列,则可以像示例中那样检查column_name:

IF NOT EXISTS(SELECT 1 FROM information_schema.columns WHERE table_name = 'tvms_routes' AND column_name = 'parentdriverid') THEN                                                    
  ALTER TABLE tvms_routes ADD COLUMN parentdriverid integer;    
END IF;                 

And can I check if attribute is already in type? 我可以检查属性是否已经在类型中?

IF NOT EXISTS(SELECT 1 FROM pg_type WHERE typname = 'tvms_dseoptitree_routes_type' AND *WHAT GOES HERE??* = 'parentvehicleid') THEN                                                     
  ALTER TYPE public.tvms_dseoptitree_routes_type ADD ATTRIBUTE parentvehicleid integer;     
END IF;     

I can't drop type, objects depends on it. 我不能删除类型,对象取决于它。 Whats the equivalent for table column_name to types attribute? 表column_name到types属性的等效项是什么?

A composite type has its corresponding table (maybe virtual). 复合类型具有其对应的表(可能是虚拟的)。 You can find its entry in pg_class and the list of its attributes in pg_attribute: 您可以在pg_class找到其条目,并在pg_attribute:找到其属性的列表pg_attribute:

IF NOT EXISTS (
    SELECT 1
    FROM pg_type t
    JOIN pg_class c ON c.oid = t.typrelid
    JOIN pg_attribute a ON a.attrelid = c.oid
    WHERE t.typname = 'tvms_dseoptitree_routes_type' 
    AND a.attname = 'parentvehicleid'
    )
THEN
    ALTER TYPE tvms_dseoptitree_routes_type ADD ATTRIBUTE parentvehicleid integer;
END IF;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM