DDC-966: SchemaTool does not check for inherited fields in STI sub-classes and overwrites their column definitions to DEFAULT NULL #1206

Closed
opened 2026-01-22 13:05:58 +01:00 by admin · 3 comments
Owner

Originally created by @doctrinebot on GitHub (Jan 1, 2011).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user ayhan:

/****
 * @Entity
 * @Table(
 *     name="eav_attribute",
 *     uniqueConstraints={
 *         @UniqueConstraint(columns={"core*project*id", "name"})
 *     }
 * )
 * @InheritanceType(
 *     "SINGLE_TABLE"
 * )
 * @DiscriminatorColumn(
 *     name="datatype",
 *     type="string"
 * )
 * @DiscriminatorMap({
 *     "datetime"="AttributeDatetime",
 *     "decimal"="AttributeDecimal",
 *     "int"="AttributeInt",
 *     "string"="AttributeString",
 *     "text"="AttributeText"
 * })
 */
abstract class AbstractAttribute
{
    // ...

    /****
     * @Column(type="string")
     */
    protected $name;

    /****
     * @Column(type="boolean")
     */
    protected $is_unique;

    // ...
}

/****
 * @Entity
 */
class AttributeDatetime extends AbstractAttribute
{
}

Expected SQL-dump:

name VARCHAR(255) NOT NULL,
is_unique TINYINT(1) NOT NULL,

SQL-dump created by SchemaTool:

name VARCHAR(255) DEFAULT NULL,
is_unique TINYINT(1) DEFAULT NULL,

This behaviour is problematic, especially for columns which are part of a unique constraint.

Reason:

SchemaTool doesn't really check for inherited fields in STI sub classes in method getSchemaFromMetadata() and passes all fields (incl. the inherited) to method _gatherColumn() where finally the column definition of the parent class will be overwritten.

private function _gatherColumn($class, array $mapping, $table)
{
    // Lines 309 - 311
    if ($class->isInheritanceTypeSingleTable() && count($class->parentClasses) > 0) {
        $options['notnull'] = false;
    }
}

A quick fix:

Change that if-clause above to

if ($class->isInheritanceTypeSingleTable() && count($class->parentClasses) > 0 && ! isset($mapping['inherited'])) {

Better fix:

Only pass not inherited fields to the _gatherColumn() method, as done with CTI sub classes.

Originally created by @doctrinebot on GitHub (Jan 1, 2011). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user ayhan: ``` /**** * @Entity * @Table( * name="eav_attribute", * uniqueConstraints={ * @UniqueConstraint(columns={"core*project*id", "name"}) * } * ) * @InheritanceType( * "SINGLE_TABLE" * ) * @DiscriminatorColumn( * name="datatype", * type="string" * ) * @DiscriminatorMap({ * "datetime"="AttributeDatetime", * "decimal"="AttributeDecimal", * "int"="AttributeInt", * "string"="AttributeString", * "text"="AttributeText" * }) */ abstract class AbstractAttribute { // ... /**** * @Column(type="string") */ protected $name; /**** * @Column(type="boolean") */ protected $is_unique; // ... } /**** * @Entity */ class AttributeDatetime extends AbstractAttribute { } ``` Expected SQL-dump: ``` name VARCHAR(255) NOT NULL, is_unique TINYINT(1) NOT NULL, ``` SQL-dump created by SchemaTool: ``` name VARCHAR(255) DEFAULT NULL, is_unique TINYINT(1) DEFAULT NULL, ``` This behaviour is problematic, especially for columns which are part of a unique constraint. Reason: SchemaTool doesn't really check for inherited fields in STI sub classes in method getSchemaFromMetadata() and passes all fields (incl. the inherited) to method _gatherColumn() where finally the column definition of the parent class will be overwritten. ``` private function _gatherColumn($class, array $mapping, $table) { // Lines 309 - 311 if ($class->isInheritanceTypeSingleTable() && count($class->parentClasses) > 0) { $options['notnull'] = false; } } ``` A quick fix: Change that if-clause above to ``` if ($class->isInheritanceTypeSingleTable() && count($class->parentClasses) > 0 && ! isset($mapping['inherited'])) { ``` Better fix: Only pass not inherited fields to the _gatherColumn() method, as done with CTI sub classes.
admin added the Bug label 2026-01-22 13:05:58 +01:00
admin closed this issue 2026-01-22 13:05:59 +01:00
Author
Owner

@doctrinebot commented on GitHub (Jan 2, 2011):

Comment created by @beberlei:

Fixed

@doctrinebot commented on GitHub (Jan 2, 2011): Comment created by @beberlei: Fixed
Author
Owner

@doctrinebot commented on GitHub (Jan 2, 2011):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (Jan 2, 2011): Issue was closed with resolution "Fixed"
Author
Owner

@doctrinebot commented on GitHub (Jan 2, 2011):

Comment created by ayhan:

Wow, that was fast!

@doctrinebot commented on GitHub (Jan 2, 2011): Comment created by ayhan: Wow, that was fast!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1206