Data Management

Cascading Delete Using Database Triggers

Summary

In relational databases, tables are often connected through relationships, and when a record is deleted from one table, it may be necessary to delete associated records from other related tables. This process is known as cascading delete, and it's an essential concept for maintaining data integrity and consistency.

In this guide, we'll explore how to implement cascading delete in Xano, a powerful no-code platform for building backend services. We'll cover two different approaches: using standard APIs and leveraging database triggers. Follow along as we walk you through the steps to efficiently manage related data across multiple tables.

Approach 1: Using Standard APIs

The first approach to implementing cascading delete in Xano involves using standard APIs. Here's how you can do it:

  1. Provide the input: First, you need to specify the record you want to delete from the primary table. This will be the input for your API.
  2. Query all related records: In Xano, use the `map` function to query all records from the related table that reference the ID of the record you want to delete from the primary table.
  3. Delete related records: Loop through the queried records and delete them from the related table using the `delete` function. You'll need to use the `id` field as the field name and the actual `id` value as the field value.
  4. Delete primary record: Finally, delete the record from the primary table using the ID you provided as the input.

Let's put this into action with an example. Suppose you have a table of companies, a table of cities, and a join table that references the IDs of companies and cities. If you want to delete a record from the companies table and also delete the corresponding records in the join table, you can follow these steps:

javascript // Query related records const relatedRecords = await db.collection('join_table').map(record => { return record.company_id === input.id; }); // Delete related records for (const record of relatedRecords) { await db.collection('join_table').delete({ id: record.id }); } // Delete primary record await db.collection('companies').delete({ id: input.id });

Approach 2: Leveraging Database Triggers

The second approach to implementing cascading delete in Xano involves leveraging database triggers. Database triggers are a powerful feature that allows you to execute custom logic whenever certain events occur in your database, such as inserting, updating, or deleting records.

Here's how you can use database triggers for cascading delete:

  1. Create a database trigger: In the Xano platform, navigate to the "Database Triggers" section and create a new trigger. Give it a name (e.g., "Delete Trigger") and specify the data source and action you want the trigger to listen for (in this case, "delete").
  2. Query related records: Inside the trigger logic, query all records from the related table that reference the ID of the record being deleted from the primary table. You can access the ID of the deleted record using the `old` object provided by the trigger.
  3. Delete related records: Loop through the queried records and delete them from the related table using the `delete` function, similar to the API approach.

Here's an example of how the trigger logic might look:

javascript const relatedRecords = await db.collection('join_table').map(record => { return record.company_id === old.id; }); for (const record of relatedRecords) { await db.collection('join_table').delete({ id: record.id }); }

By leveraging database triggers, you can automate the cascading delete process and ensure that related data is properly cleaned up whenever a record is deleted from the primary table.

Both approaches have their advantages and use cases. Using standard APIs allows you to have more control over the cascading delete process and integrate it into your existing application logic. Database triggers, on the other hand, provide a more automated and transparent way of handling cascading delete, as the logic is executed directly within the database.

Regardless of the approach you choose, Xano's no-code platform makes it easy to implement cascading delete and maintain data integrity across your applications. Follow the step-by-step instructions provided in this guide, and you'll be able to efficiently manage related data across multiple tables, ensuring a consistent and reliable data management experience.

This transcript was AI generated to allow users to quickly answer technical questions about Xano.

Was this helpful?

I found it helpful

I need more support
Sign up for XanoSign up for Xano

Build without limits on a secure, scalable backend.

Unblock your team's progress and create a backend that will scale for free.

Start building for free