# Relationship & Trigger

**URL:** <https://forums.toadworld.com/t/relationship-trigger/32515>\
**Category:** Toad Data Modeler\
**Created:** [February 4, 2015, 7:41am UTC](https://forums.toadworld.com/t/relationship-trigger/32515 "2015-02-04T07:41:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![andreab01](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@andreab01](https://forums.toadworld.com/u/andreab01)\
**Post date:** [February 4, 2015, 7:41am UTC](https://forums.toadworld.com/t/relationship-trigger/32515/1 "2015-02-04T07:41:02Z")

</div>

Hi,

My current data modeler is Erwin and I’m evaluating Toad Data Modeler.

I’d like to know if it is possible to generate triggers from the relationships instead of creating foreign key constraints.

For example, if I define a relationship with cascade delete between my\_table and my\_table\_2, I’d like to have an auto-generated trigger such as:

create trigger [dbo].[tD\_my\_table] on [dbo].[my\_table] for DELETE as  
begin  
delete my\_table\_2  
from my\_table\_2,deleted  
where  
my\_table\_2.fk\_my\_table\_pk = deleted.my\_table\_pk  
end

---

<div class="post-metadata">

**Author:** ![Lukas.Knapek](https://sea1.discourse-cdn.com/quest/user_avatar/forums.toadworld.com/lukas.knapek/32/269_2.png) [@Lukas.Knapek](https://forums.toadworld.com/u/Lukas.Knapek)\
**Post date:** [February 5, 2015, 1:02am UTC](https://forums.toadworld.com/t/relationship-trigger/32515/2 "2015-02-05T01:02:25Z")

</div>

Hello Andrea,

Thank you for your interest. I’m afraid referential integrity triggers are currently not supported in Toad Data Modeler. However, you can always put your feature ideas into the Idea Pond. The most voted ideas are reviewed by the developers and most of them get implemented in future versions of Toad Data Modeler.

Regards,

Lukas

---

<div class="post-metadata">

**Author:** ![andreab01](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@andreab01](https://forums.toadworld.com/u/andreab01)\
**Post date:** [August 26, 2020, 4:33pm UTC](https://forums.toadworld.com/t/relationship-trigger/32515/3 "2020-08-26T16:33:53Z")

</div>

Hi,  
Is there any new implementation regarding my request?  
Alternatively, is there a workaround, like some macro?  
Thanks  
Andrea

---

<div class="post-metadata">

**Author:** ![Daril](https://sea1.discourse-cdn.com/quest/user_avatar/forums.toadworld.com/daril/32/31_2.png) [@Daril](https://forums.toadworld.com/u/Daril)\
**Post date:** [August 28, 2020, 7:39am UTC](https://forums.toadworld.com/t/relationship-trigger/32515/4 "2020-08-28T07:39:05Z")

</div>

Hello Andrea,  
there are no native implementation of generation triggers for a referential integrity, but it is possible by customization.  
There are two possible ways.

1. Create macro, which will create triggers in TDM models according to relationships. In this case you run the macro and it will create update/delete triggers in your model by the current state. You can manually edit these triggers etc. Disadvantage is that these triggers are not bind to relationships, so any change in relationship dwill not change triggers automatically.

2. Rewrite Generator to generate sql code for create triggers instead foreign keys. In this case it will generate create statements for triggers each time when you generate SQL code for relationships. Triggers will not be in model, but it will be generated each time by actual state in model.

Presume that you are use SQL Server.  
I attached zip archiv. In this zip you can find

- CreateRITriggersOR.txg
- CreateRITriggersMS.txg
- GeneratorRIEntityTriggers.txg

Zip archiv download here [ReferentialIntegrity.zip](https://dl.bintray.com/quest/ToadDataModeler/Forum/ReferentialIntegrity.zip?expiry=1609489886733&id=IRrec6NpYhLV52JsVcWVBAsl%2BE9REFOeWMVypt8PzT0%3D&signature=EcPYF88wyn%2BBV7xYBHY%2BR0sQ1zSsyhKGdEo%2FN3zQQeY870JqQrfVVPjZODtKEXrKMBFW53JzcXM6eZn5JLy%2Bzg%3D%3D)  
This is just draft not finished packages. You need modify it by your preferences.

**CreateRITriggersOR.txg**  
This package contains macro which can create triggers for relationship. You can right click on relationship on designer and in popup menu you will see this macro. This macro is available too from main menu - Macros. In this case it will create triggers for all relationships in model.

**CreateRITriggersMS.txg**  
This packages is just conversion previous to MS. It has not fully changed for some SQL server specific, use it as base if you would like go by this way.

**GeneratorRIEntityTriggers.txg**  
This package contains basic modification for generator of SQL Server. Instead code for foreign keys it will generate "Code for triggers". You need modify method below to get requested result.

```auto
function CreateRelation(relation)
{
  return "Code for triggers";
};

```

Note: For customization in TDM you need switch on expert mode from settings.

if you have some questions please don't hesitate to ask us.

Best regards Petr
