PostgreSQL
 sql >> Datenbank >  >> RDS >> PostgreSQL

RÜCKSENDUNG von Daten aus der aktualisierbaren Ansicht funktioniert nicht?

Sie waren so nah dran;) Sie müssen nur NEW.id aktualisieren in Ihrem Trigger.

CREATE FUNCTION t0ii_person_details() RETURNS trigger AS $$
DECLARE
  addr  integer;
BEGIN
  -- Want at least a town to insert anything into table address.
  IF (NEW.town IS NOT NULL) THEN
    INSERT INTO address(street, town)
      VALUES (NEW.street, NEW.town)
      RETURNING id INTO addr;
  ELSE
    addr := NULL;
  END IF;

  INSERT INTO person(first_name, family_name, address)
    VALUES (NEW.first_name, NEW.family_name, addr) 
    -- here is the only change I made
    RETURNING id INTO NEW.id;
  RETURN NEW;
END; $$ LANGUAGE plpgsql SECURITY DEFINER;