strange foreign keys error

  • Hi.

    I wanted to create table describing hierarchy between various groups in our system. The table:

    create table group_2_group (

    sup_group int not null,

    sub_group int not null,

    primary key CLUSTERED (sup_group, sub_group),

    foreign key (sup_group) references groups (group_id) ON DELETE CASCADE ON UPDATE CASCADE,

    foreign key (sub_group) references groups (group_id) ON DELETE CASCADE ON UPDATE CASCADE

    )

    surprisingly returned error.

    Introducing FOREIGN KEY constraint 'FK__rys_group__sub_g__1BC821DD' on table 'rys_group_2_group' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

    In this particular situation it does not make much sense - how can i work around it?

  • You've got two foreign keys that are going to try to cascade an update or delete on the same table... It's a circular reference.

    Since you're on 2008, why don't you use the new hiearchy data type?

    ----------------------------------------------------The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood... Theodore RooseveltThe Scary DBAAuthor of: SQL Server 2017 Query Performance Tuning, 5th Edition and SQL Server Execution Plans, 3rd EditionProduct Evangelist for Red Gate Software

  • HierarchyID is good for defining trees but actually it is incredibly unpleasent when it comes to rearrange the tree. And hierarchy here is not a tree - it's just DAG.

    And no - it's not circular reference as far as I understand circular reference. No table is referencing this one. I just want to delete records from the table in both possible situations.

  • i think only one of the two foreign keys should have the cascade options; that would resolve your circular reference issue, right?

    the second column should reference, but not delete related data?

    create table group_2_group (

    sup_group int not null,

    sub_group int not null,

    primary key CLUSTERED (sup_group, sub_group),

    foreign key (sup_group) references groups (group_id) ON DELETE CASCADE ON UPDATE CASCADE,

    foreign key (sub_group) references groups (group_id)

    )

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Of course this will make my SQL Statement valid but the problem is that I have to put additional trigger on the table.

    The use -case scenario is like

    INSERT INTO groups (group_id, group_name) VALUES (1,'LEADERS');

    INSERT INTO groups (group_id, group_name) VALUES (2,'WORKERS');

    INSERT INTO group_2_group (sub_group, sup_group) VALUES (2,1);

    DELETE FROM groups WHERE group_id = 2

    and here - with only following foreign key:

    ( sup_group ) references groups (group_id) on delete cascade

    I will have incomplete structure in db.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply