Details
-
Type:
Bug
-
Status:
Resolved
-
Priority:
Critical
-
Resolution: Fixed
-
Affects Version/s: None
-
Fix Version/s: 2.0-BETA3
-
Component/s: None
-
Security Level: All
-
Labels:None
Description
I have two Entities: Users and Alerts. They are associated with @ManyToMany, the assiciation table should be "user_alert".
Entities:
http://pastebin.com/m7bca724a
Sample code:
http://pastebin.com/m4e530f42
The "remove" command in the sample code deletes the user entry and alert entry, but not the user_alert entry (which was automatically created though). This leaves an orphan entry (or the DBMS will complain because of FK constraints).
INSERT INTO users (name) VALUES (?)
array(1) {
[1]=>
string(3) "Bob"
}
INSERT INTO alert (name) VALUES (?)
array(1) {
[1]=>
string(9) "Testalert"
}
INSERT INTO user_alert (userId, alertId) VALUES (?, ?)
array(2) {
[0]=>
int(1)
[1]=>
int(1)
}
DELETE FROM users WHERE id = ?
array(1) {
[0]=>
int(1)
}
DELETE FROM alert WHERE id = ?
array(1) {
[0]=>
int(1)
}
The user_alert table should be automatically updated (on creation and removal)...
Issue Links
- relates to
-
DDC-677
Allow DQL DELETE statements to work with join table fk constraints
-
I think in most cases the entry in the association table should be deleted by the FK constraint. However I think that is currently not the default. You can force it by using onDelete="CASCADE" on the @JoinColumn definitions inside the @JoinTable. That should probably be set by default on join columns on an association table.
I'm not sure its worth supporting manual deletion of association table entries through Doctrine as all databases support proper foreign key constraints and this is the most effective way to delete these entries and also enforces the integrity on the database side.
So I'm rather voting for making onDelete="CASCADE" the default for join columns of association tables unless specified otherwise.