Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
Back to tool/Back to issues
#12609·orm

3.7.0 SchemaTool silently drops FK for conflicting STI associations targeting the same entity

Author: HypeMCCreated Sep 12, 2026Updated Sep 18, 2026

Bug Report

Q A
Version 3.7.0
Previous Version if the bug is a regression 3.6.8

Summary

When multiple child entities in a single-table inheritance hierarchy define an association using the same join column and target the same entity, but use different JoinColumn configuration, SchemaTool silently drops the foreign key.

This appears to have been introduced by https://github.com/doctrine/orm/pull/12528.

There is a related case in https://github.com/doctrine/orm/issues/12606 where the same join column is used for associations targeting different entities. In that case, not creating a foreign key makes sense because there is no single FK constraint that can represent the mapping.

However, when the target entity is the same and only the association configuration differs, silently ignoring the FK seems problematic. The mapping is effectively inconsistent, so it might be better to reject it instead.

Current behavior

With Doctrine ORM 3.6.8, the generated schema contains the foreign key:

sql
CREATE TABLE base (
    id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    dtype VARCHAR(255) NOT NULL,
    ref_id INT NOT NULL,
    PRIMARY KEY (id)
);
CREATE INDEX IDX_C0B4FE6121B741A9 ON base (ref_id);

CREATE TABLE ref (
    id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    PRIMARY KEY (id)
);

ALTER TABLE base
    ADD CONSTRAINT FK_C0B4FE6121B741A9
    FOREIGN KEY (ref_id) REFERENCES ref (id) NOT DEFERRABLE;

With Doctrine ORM 3.7.0, the same mapping generates:

sql
CREATE TABLE base (
    id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    dtype VARCHAR(255) NOT NULL,
    ref_id INT DEFAULT NULL,
    PRIMARY KEY (id)
);
CREATE INDEX IDX_C0B4FE6121B741A9 ON base (ref_id);

CREATE TABLE ref (
    id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    PRIMARY KEY (id)
);

The foreign key is silently omitted.

Expected behavior

I am not sure that using either child entity's configuration would necessarily be correct, since the two associations provide conflicting metadata.

However, because both associations target the same entity, silently dropping the foreign key seems unexpected.

Perhaps Doctrine should detect that the same join column and target entity are configured differently and throw a mapping exception instead?

@greg0ire WDYT?

How to reproduce

A simplified mapping looks like this:

php
#[ORM\Entity]
#[ORM\InheritanceType('SINGLE_TABLE')]
#[ORM\DiscriminatorColumn(name: 'dtype', type: 'string')]
#[ORM\DiscriminatorMap([
    'one' => ChildOne::class,
    'two' => ChildTwo::class,
    'three' => ChildThree::class,
])]
class Base
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;
}

#[ORM\Entity]
class ChildOne extends Base
{
    #[ORM\ManyToOne(targetEntity: Ref::class)]
    #[ORM\JoinColumn(name: 'ref_id', nullable: false)]
    private Ref $ref;
}

#[ORM\Entity]
class ChildTwo extends Base
{
}

#[ORM\Entity]
class ChildThree extends Base
{
    #[ORM\ManyToOne(targetEntity: Ref::class)]
    #[ORM\JoinColumn(name: 'ref_id', nullable: true)]
    private ?Ref $ref = null;
}

#[ORM\Entity]
class Ref
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;
}

The important part is that ChildOne and ChildThree use the same ref_id column and target the same Ref entity, while their join column configuration differs.

On 3.6.8, SchemaTool generates a foreign key for ref_id. On 3.7.0, the conflict causes the foreign key to be ignored entirely.

Source: doctrine/orm

View original on GitHubView discussion on GitHub