pgschema
pgschema copied to clipboard
Error when dropping a table with a trigger
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!
This should have been fixed in 1.12.1