简体   繁体   中英

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'.

If I'm adding a column to the table, I can check the column_name as in example:

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?

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:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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