Questions tagged [cascading-deletes]

The concept of SQL databases deleting rows that refer (via a foreign key) to a deleted row.

The concept of SQL databases deleting rows that refer (via a foreign key) to a deleted row. The deletion is performed recursively and enabled with the ON DELETE CASCADE property of the foreign key.

587 questions
389
votes
8 answers

How do I use cascade delete with SQL Server?

I have 2 tables: T1 and T2, they are existing tables with data. We have a one to many relationship between T1 and T2. How do I alter the table definitions to perform cascading delete in SQL Server when a record from T1 is deleted, all associated…
Bichvan Nguyen
  • 4,039
  • 2
  • 18
  • 13
261
votes
3 answers

On delete cascade with doctrine2

I'm trying to make a simple example in order to learn how to delete a row from a parent table and automatically delete the matching rows in the child table using Doctrine2. Here are the two entities I'm using: Child.php:
rfc1484
  • 9,441
  • 16
  • 72
  • 123
241
votes
5 answers

How to add "on delete cascade" constraints?

In PostgreSQL 8 is it possible to add ON DELETE CASCADES to the both foreign keys in the following table without dropping the latter? # \d scores Table "public.scores" Column | Type |…
231
votes
8 answers

How does JPA orphanRemoval=true differ from the ON DELETE CASCADE DML clause

I am a little confused about the JPA 2.0 orphanRemoval attribute. I think I can see it is needed when I use my JPA provider's DB generation tools to create the underlying database DDL to have an ON DELETE CASCADE on the particular relation. However,…
Markos Fragkakis
  • 7,499
  • 18
  • 65
  • 103
137
votes
6 answers

Delete rows with foreign key in PostgreSQL

I would like to delete rows which contain a foreign key, but when I try something like this: DELETE FROM osoby WHERE id_osoby='1' I get this statement: ERROR: update or delete on table "osoby" violates foreign key constraint "kontakty_ibfk_1" on…
Michal Loksik
  • 1,609
  • 3
  • 14
  • 19
136
votes
5 answers

What is the difference between CascadeType.REMOVE and orphanRemoval in JPA?

What's the difference between @OneToMany(cascade=REMOVE, mappedBy="customer") public List getOrders() { ... } and @OneToMany(mappedBy="customer", orphanRemoval="true") public List getOrders() { ... } This example is from Java EE…
rand0m86
  • 3,172
  • 4
  • 26
  • 29
84
votes
1 answer

Doctrine: cascade="remove" vs orphanRemoval=true

What is the difference between the 2 options above? When is it preferable to choose each option?
iiirxs
  • 4,493
  • 2
  • 20
  • 35
78
votes
1 answer

CoreData + iCloud + Cascade Delete - how to handle?

CoreData Entity "A" has a one-to-many relationship to a collection of CoreData Entries "B", using a Cascade delete rule. In an iCloud environment, while device 1 shows a detail view of one of the "B" entries, device 2 deletes the "A" entry. When the…
Amiram Stark
  • 2,208
  • 22
  • 32
68
votes
3 answers

What are the options for overriding Django's cascading delete behaviour?

Django models generally handle the ON DELETE CASCADE behaviour quite adequately (in a way that works on databases that don't support it natively.) However, I'm struggling to discover what is the best way to override this behaviour where it is not…
Tom
  • 42,844
  • 35
  • 95
  • 101
59
votes
3 answers

Entity Framework (EF) Code First Cascade Delete for One-to-Zero-or-One relationship

Following the "Code First Modeling" section of the Pluralsight "Getting Started with Entity Framework 5" course by Julie Lerman, I created two POCO classes with a one-to-zero-or-one relationship: a parent (User) and an optional child…
50
votes
6 answers

JPA/Hibernate remove entity sometimes not working

I have the following code that usually works well: public void delete(T object) { EntityManager em = getPersistence().createEntityManager(); EntityTransaction et = em.getTransaction(); try { et.begin(); object =…
Lucio Crusca
  • 1,277
  • 3
  • 15
  • 41
46
votes
4 answers

SQLite Delete Cascade not working

In Android 4.2, using SQLite 3.7.11, when I delete a row from the Quizzes table, who's schema is below, the corresponding rows in the QuizQuestions table are not deleted. I can't figure out what's wrong. I have tried putting db.execSQL("PRAGMA…
Dan14021
  • 639
  • 1
  • 9
  • 15
34
votes
5 answers

Is it possible to delete from multiple tables in the same SQL statement?

It's possible to delete using join statements to qualify the set to be deleted, such as the following: DELETE J FROM Users U inner join LinkingTable J on U.id = J.U_id inner join Groups G on J.G_id = G.id WHERE G.Name = 'Whatever' and U.Name not in…
bwerks
  • 8,651
  • 14
  • 68
  • 100
32
votes
2 answers

Entity Framework 4.1 InverseProperty Attribute and ForeignKey

I will create two references between Employee and Team entities with foreign keys. So I defined two entities as follow public class Employee { public int EmployeeId { get; set; } public string Name { get; set; } …
Ray
  • 4,038
  • 8
  • 36
  • 48
32
votes
2 answers

What is the recommended equivalent of cascaded delete in MongoDB for N:M relationships?

Assuming the following "schema/relationship" design what is the recommended practice for handling deletion with cascade delete like operation? Relational Schema: +---------+ +--------+ | Student…
1
2 3
39 40