Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8301123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:49:24+00:00 2026-06-08T16:49:24+00:00

I have a table, call it EVENTS, where each row can depend on 0

  • 0

I have a table, call it EVENTS, where each row can depend on 0 or more other rows in the table. I need a way of representing this relationship that also prevents circular dependencies (i.e. a group of events leading back into an event in that same group).

I currently have a link table external to EVENTS, call it EVENTS_DEP. This table links dependent rows to the rows they depend on and allows for multiple dependencies on one row. How would I prevent circular dependencies using such a table?

NOTE: if it is at all possible to do this only with the database design (not with scripts, triggers, etc.), this would be ideal.

Also, if this can only be done using triggers, please let me know what kind of trigger (i.e. on what event) it should be run on (on insert, maybe?).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T16:49:25+00:00Added an answer on June 8, 2026 at 4:49 pm

    INSERT Trigger to check this.

    Assuming the following table structure

    CREATE TABLE event (
        id bigserial PRIMARY KEY,
        foo varchar
    );
    
    CREATE TABLE event_deps (
        parent bigint REFERENCES event(id),
        child bigint REFERENCES event(id),
    
        PRIMARY KEY (parent, child),
        CHECK (parent <> child)
    );
    

    The following INSERT trigger would be needed

    CREATE FUNCTION deps_insert_trigger_func() RETURNS trigger AS $BODY$
        DECLARE
            results bigint;
        BEGIN
            WITH RECURSIVE p(id) AS (
                SELECT parent
                    FROM event_deps
                    WHERE child=NEW.parent
                UNION
                SELECT parent
                    FROM p, event_deps d
                    WHERE p.id = d.child
                )
            SELECT * INTO results
            FROM p
            WHERE id=NEW.child;
    
            IF FOUND THEN 
                RAISE EXCEPTION 'Circular dependencies are not allowed.';
            END IF;
            RETURN NEW;
        END;
    $BODY$ LANGUAGE plpgsql;
    
    CREATE TRIGGER before_insert_event_deps_trg BEFORE INSERT ON event_deps
        FOR EACH ROW
        EXECUTE PROCEDURE deps_insert_trigger_func();
    

    What it does is when a new link is added between parent A and child B it uses A WITH RECURSIVE query to find all ancestors of A. B shouldn’t be one of them.

    The UPDATE trigger is harder because when the trigger is executed to old link is still there so the test from the INSERT trigger could give a false positive when used for UPDATEs.

    So for the UPDATE we need to add some extra conditions to hide the old data.

    CREATE FUNCTION deps_update_trigger_func() RETURNS trigger AS $BODY$
        DECLARE
            results bigint;
        BEGIN
            WITH RECURSIVE p(id) AS (
                SELECT parent
                    FROM event_deps
                    WHERE child=NEW.parent
                        AND NOT (child = OLD.child AND parent = OLD.parent) -- hide old row
                UNION
                SELECT parent
                    FROM p, event_deps d
                    WHERE p.id = d.child
                        AND NOT (child = OLD.child AND parent = OLD.parent) -- hide old row
                )
            SELECT * INTO results
            FROM p
            WHERE id=NEW.child;
    
            IF FOUND THEN 
                RAISE EXCEPTION 'Circular dependencies are not allowed.';
            END IF;
            RETURN NEW;
        END;
    $BODY$ LANGUAGE plpgsql;
    
    CREATE TRIGGER before_update_event_deps_trg BEFORE UPDATE ON event_deps
        FOR EACH ROW
        EXECUTE PROCEDURE deps_update_trigger_func();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table (call it table myTable). It has the data like this:
I have a large (hundreds of thousands of rows) table (call it dataPoints). Another
I have a table that contains in each row an input and a save
I have a table call lp_upload and it contain plate number of car and
I have a simple table (lets call it Table1) that has a NVARCHAR field
I have a table, let's call is [MYTABLE] , with an FOR INSERT, UPDATE
I have to call the table() function on 10 variables in R. Is there
I have a jQuery ajax call that returns html of a table. Now I
I have problems filling a temporary table with the resultset from a procedure call
I have two quite large tables, let's call them 'authors' and 'articles'. Every row

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.