DDC-3134: Inconsistent defaults for onDelete when defining many-to-many relations #3886

Open
opened 2026-01-22 14:30:32 +01:00 by admin · 0 comments
Owner

Originally created by @doctrinebot on GitHub (May 23, 2014).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user adlpz:

When defining a relation with the following annotation:

/****
 * @ORM\ManyToMany(targetEntity="A\B\C")
 */

The following SQL is generated for the foreign key constraints:

ALTER TABLE the*table ADD CONSTRAINT FK*A63A409AA76ED395 FOREIGN KEY (something1) REFERENCES thing2 (id) ON DELETE CASCADE;
ALTER TABLE the*table ADD CONSTRAINT FK*A63A409AF8BD700D FOREIGN KEY (something2) REFERENCES thing1 (id) ON DELETE CASCADE;

As it can be seen, the default seems to be to include ON DELETE CASCADE. This feels like a dangerous behaviour (and I couldn't find it documented), but fair enough. The inconsistency comes when adding:

/****
 * @ORM\ManyToMany(targetEntity="A\B\C")
 * @ORM\JoinTable(name="the_table",
 *      joinColumns={@ORM\JoinColumn()},
 *      inverseJoinColumns={@ORM\JoinColumn()}
 * )
 */

In this case, even if we are not providing any values for the join and inverse join columns, we get:

ALTER TABLE the*table ADD CONSTRAINT FK*A63A409AA76ED395 FOREIGN KEY (something1) REFERENCES something2 (id);
ALTER TABLE user*unit ADD CONSTRAINT FK*A63A409AF8BD700D FOREIGN KEY (something2) REFERENCES something1 (id);

As it can be seen, the ON DELETE CASCADE is gone.

I understand this is the case because, when giving empty JoinColumn annotations it's not the engine defaults that are processed, but the inferred values for the annotation. Still, this seems to be either a bug or an undocumented behaviour.

Am I doing anything wrong here? Should this be looked into?

Originally created by @doctrinebot on GitHub (May 23, 2014). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user adlpz: When defining a relation with the following annotation: ``` java /**** * @ORM\ManyToMany(targetEntity="A\B\C") */ ``` The following SQL is generated for the foreign key constraints: ``` sql ALTER TABLE the*table ADD CONSTRAINT FK*A63A409AA76ED395 FOREIGN KEY (something1) REFERENCES thing2 (id) ON DELETE CASCADE; ALTER TABLE the*table ADD CONSTRAINT FK*A63A409AF8BD700D FOREIGN KEY (something2) REFERENCES thing1 (id) ON DELETE CASCADE; ``` As it can be seen, the default seems to be to include **ON DELETE CASCADE**. This feels like a dangerous behaviour (and I couldn't find it documented), but fair enough. The inconsistency comes when adding: ``` java /**** * @ORM\ManyToMany(targetEntity="A\B\C") * @ORM\JoinTable(name="the_table", * joinColumns={@ORM\JoinColumn()}, * inverseJoinColumns={@ORM\JoinColumn()} * ) */ ``` In this case, even if we are not providing any values for the join and inverse join columns, we get: ``` sql ALTER TABLE the*table ADD CONSTRAINT FK*A63A409AA76ED395 FOREIGN KEY (something1) REFERENCES something2 (id); ALTER TABLE user*unit ADD CONSTRAINT FK*A63A409AF8BD700D FOREIGN KEY (something2) REFERENCES something1 (id); ``` As it can be seen, the **ON DELETE CASCADE** is gone. I understand this is the case because, when giving empty **JoinColumn** annotations it's not the engine defaults that are processed, but the inferred values for the annotation. Still, this seems to be either a bug or an undocumented behaviour. Am I doing anything wrong here? Should this be looked into?
admin added the Bug label 2026-01-22 14:30:32 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3886