doctrine / doctrine/orm

Cascade remove tries to delete child entities with wrong colum name for fk column

Open
#6,331 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
PHP
Stars
10.2k
Forks
2.5k
Avg merge
1d 10h
Merged PRs (30d)
34

Description

During testing our entities, I've found that a OneToMany relationship with `cascade={"persist", "remove"}` successfully persists child entities, but fails to remove them with a DBALException.

Parent Entity:
```class Scene implements CreateableInterface, SceneConnectable
{
use Creator;
use Deletor;
use SceneBasics;
/** @Id @Column(type="integer") @GeneratedValue */
private $id;
/**
* @OneToMany(targetEntity="SceneConnectionGroup", mappedBy="scene", cascade={"persist", "remove"})
*/
private $connectionGroups = null;
}
```

Child Entity:
```
class SceneConnectionGroup implements SceneConnectable
{
/**
* @Id
* @ManyToOne(targetEntity="Scene", inversedBy="outgoingConnections", cascade={"persist"})
* @JoinColumn(name="scene_id", referencedColumnName="id")
*/
private $scene;
/**
* @Id
* @Column(type="string")
*/
private $name;
}
```

Removal of the parent entity results in an exception:

```
Doctrine\DBAL\DBALException: An exception occurred while executing 'DELETE FROM scene_connection_groups WHERE scene = ? AND name = ?' with params [null, "lotgd\/module-village\/outside"]:
```

The issue lies in the naming of the fk column: Doctrine tries to use the colum name "scene", which does not exist in the table. Instead, the column is actually called scene_id as specified in `@JoinColumn`. Removal of the `@JoinColumn` does not change the result (it still throws the exception).

Renaming the join column to "scene" circumvents this issue. We use currently the most recent version (2.5.6) on PHP 7.1.

Contributor guide

Open the contributing guide

Research direction

Start with the parent and child entity mappings in the issue, then reproduce the cascade remove operation on Doctrine ORM 2.5.6 with PHP 7.1 and inspect the generated DELETE statement. Done means the child delete uses the JoinColumn name `scene_id` rather than the property name `scene`, while preserving the composite-key behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.