I am trying to add a column to one of my existing tables, but keep getting the following error at this place:
ALTER TABLE fact_parkingtransaction ADD COLUMN dateinserted timestamp without time zone NOT NULL;
ERROR: column "dateinserted" contains null values
What does this exactly mean? Postgres doesn't let me create an empty column because the rows will already exist if the column is created or what am I getting wrong?
Here's my script:
do $$
declare
arow record;
curtable varchar(50);
begin
IF EXISTS (SELECT relname FROM pg_class WHERE relname='durations') THEN
-- DO STUFF HERE
-- DROP OLD TRIGGER
DROP TRIGGER IF EXISTS durations_partition_trigger ON durations;
ALTER SEQUENCE IF EXISTS durations_id_seq RENAME TO fact_parkingtransaction_id_seq;
-- Rename events table to fact_entriesexits
ALTER TABLE durations RENAME TO fact_parkingtransaction;
ALTER TABLE fact_parkingtransaction rename column duration to duration_old; -- new value will be set later on
-- Add new columns
ALTER TABLE fact_parkingtransaction ADD COLUMN entryfacilitykey integer;
ALTER TABLE fact_parkingtransaction ADD COLUMN exitfacilitykey integer;
ALTER TABLE fact_parkingtransaction ADD COLUMN systeminterfacekey integer;
ALTER TABLE fact_parkingtransaction ADD COLUMN manufacturerkey integer;
ALTER TABLE fact_parkingtransaction ADD COLUMN tickettypekey integer;
ALTER TABLE fact_parkingtransaction ADD COLUMN entrydatekey integer;
ALTER TABLE fact_parkingtransaction ADD COLUMN entrytimekey integer;
ALTER TABLE fact_parkingtransaction ADD COLUMN exitdatekey integer;
ALTER TABLE fact_parkingtransaction ADD COLUMN exittimekey integer;
ALTER TABLE fact_parkingtransaction ADD COLUMN entrydevicekey integer;
ALTER TABLE fact_parkingtransaction ADD COLUMN exitdevicekey integer;
ALTER TABLE fact_parkingtransaction ADD COLUMN entrytime timestamp without time zone;
ALTER TABLE fact_parkingtransaction ADD COLUMN exittime timestamp without time zone;
ALTER TABLE fact_parkingtransaction ADD COLUMN duration integer;
ALTER TABLE fact_parkingtransaction ADD COLUMN eventid_arrival bigint;
ALTER TABLE fact_parkingtransaction ADD COLUMN eventid_departure bigint;
ALTER TABLE fact_parkingtransaction ADD COLUMN cardnumber character varying(100);
ALTER TABLE fact_parkingtransaction ADD COLUMN licenseplate character varying(100);
ALTER TABLE fact_parkingtransaction ADD COLUMN licenseplatekey integer;
ALTER TABLE fact_parkingtransaction ADD COLUMN dateinserted timestamp without time zone NOT NULL;
ALTER TABLE fact_parkingtransaction ADD COLUMN etlsource integer;
-- Rename events_yyyy_mm to fact_entriesexits_yyyymm
for arow in
SELECT table_name FROM information_schema.tables WHERE table_schema='public' and table_name like 'durations_%'
loop
curtable := arow.table_name;
RAISE NOTICE 'Calling table(%)', curtable;
--Rename
execute ('ALTER TABLE ' || curtable ||' RENAME TO fact_parkingtransaction_' || replace(right(curtable, 8), '_', ''));
--Add idx on datekey
execute('CREATE INDEX idx_fact_parkingtransaction_' || replace(right(curtable, 8), '_', '') || ' ON fact_parkingtransaction_' || replace(right(curtable, 8), '_', '') || ' ( datekey )');
end loop;
-- END RENAME PARTITION
-- Update to 2.0 Mapping
-- TICKET TYPES
Update fact_parkingtransaction e set ticket_type = 14 where ticket_type in (2,15);
Update fact_parkingtransaction e set ticket_type = 41 where ticket_type = 8;
Update fact_parkingtransaction e set ticket_type = 9 where ticket_type = 30;
Update fact_parkingtransaction e set ticket_type = 21 where ticket_type = 20;
Update fact_parkingtransaction e set ticket_type = 33 where ticket_type = 18;
-- fill new fields
Update fact_parkingtransaction e
set entrydatekey = cast(to_char((event_time_arrival)::TIMESTAMP,'yyyymmdd') as integer),
entrytimekey = cast(to_char((event_time_arrival)::TIMESTAMP,'hhmiss') as integer),
exitdatekey = cast(to_char((event_time_departure)::TIMESTAMP,'hhmiss') as integer),
exittimekey = cast(to_char((event_time_departure)::TIMESTAMP,'yyyymmdd') as integer),
entrytime = event_time_arrival,
exittime = event_time_departure,
duration = duration_old,
eventid_arrival = event_id_arrival,
eventid_departure = event_id_departure,
cardnumber = card_nr,
manufacturerkey =
CASE
WHEN manufacturer LIKE '%IPCP%' THEN 1
WHEN manufacturer LIKE '