pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

Error when dropping a table with a trigger

Open lebedev-nikita opened this issue 3 months ago • 1 comments

Initial schema

-- schema.sql
CREATE TABLE items (
  id integer PRIMARY KEY,
  label text NOT NULL
);

CREATE FUNCTION stamp_item()
RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
  RETURN NEW;
END;
$$;

CREATE TRIGGER stamp_item
BEFORE INSERT ON items
FOR EACH ROW
EXECUTE FUNCTION stamp_item();

it runs successfully: pgschema apply --auto-approve --file schema.sql

Edited schema

We decided to remove the table "items" and its trigger:

-- schema.sql

-- CREATE TABLE items (
--   id integer PRIMARY KEY,
--   label text NOT NULL
-- );

-- CREATE FUNCTION stamp_item()
-- RETURNS trigger
-- LANGUAGE plpgsql
-- AS $$
-- BEGIN
--   RETURN NEW;
-- END;
-- $$;

-- CREATE TRIGGER stamp_item
-- BEFORE INSERT ON items
-- FOR EACH ROW
-- EXECUTE FUNCTION stamp_item();

running pgschema apply --auto-approve --file schema.sql and we get an error:

DDL to be executed:
--------------------------------------------------

DROP FUNCTION IF EXISTS stamp_item();

DROP TABLE IF EXISTS items CASCADE;

Applying changes...

Executing group 1/1...
  Executing 2 statements in implicit transaction
Error: failed to execute concatenated statements in group 1: ERROR: cannot drop function stamp_item() because other objects depend on it (SQLSTATE 2BP01)

Note

if we drop only the trigger and a function

-- schema.sql
CREATE TABLE items (
  id integer PRIMARY KEY,
  label text NOT NULL
);

-- CREATE FUNCTION stamp_item()
-- RETURNS trigger
-- LANGUAGE plpgsql
-- AS $$
-- BEGIN
--   RETURN NEW;
-- END;
-- $$;

-- CREATE TRIGGER stamp_item
-- BEFORE INSERT ON items
-- FOR EACH ROW
-- EXECUTE FUNCTION stamp_item();

it runs successfully: pgschema apply --auto-approve --file schema.sql

DDL to be executed:
--------------------------------------------------

DROP TRIGGER IF EXISTS stamp_item ON items;

DROP FUNCTION IF EXISTS stamp_item();

Applying changes...

Executing group 1/1...
  Executing 2 statements in implicit transaction
Changes applied successfully!

lebedev-nikita avatar Aug 13 '26 09:08 lebedev-nikita

This should have been fixed in 1.12.1

tianzhou avatar Aug 14 '26 11:08 tianzhou