DDC-3423: Column Ordering when creating tables using doctrine:schema:update #4224

Open
opened 2026-01-22 14:37:39 +01:00 by admin · 3 comments
Owner

Originally created by @doctrinebot on GitHub (Dec 1, 2014).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user rajasaur:

When you have a Base class (annotated with @MappedSuperClass) having some columns, say, createdById, createdDateTime and child entities deriving from the BaseClass, the ordering of the SQL when running doctrine:schema:update looks like

createdById
createdDateTime
id
field1
field2

i.e. the columns of the Base class come up first and then that of the children. It looks odd when you write a SQL to insert into these tables because the default ordering is not what you expect (Which would be derived class columns first and then base class).

I looked into ClassMetadataFactory that adds the fields to the classmetadata and figured if we could move the following
$this->addInheritedFields($class, $parent);
$this->addInheritedRelations($class, $parent);

to after the point where the local fields are added to the classmetadata, this problem is solved.

It might be even better if we have an annotation to specify the Ordering of columns but nevertheless this will help in cases where the base class columns appear after the derived class columns.

Note: Did look into columnDefinition annotation to specify a "AFTER ", but that cannot be used during CREATE TABLE, only for ALTER TABLE and that too, its mysql specific.

Originally created by @doctrinebot on GitHub (Dec 1, 2014). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user rajasaur: When you have a Base class (annotated with @MappedSuperClass) having some columns, say, createdById, createdDateTime and child entities deriving from the BaseClass, the ordering of the SQL when running doctrine:schema:update looks like createdById createdDateTime id field1 field2 i.e. the columns of the Base class come up first and then that of the children. It looks odd when you write a SQL to insert into these tables because the default ordering is not what you expect (Which would be derived class columns first and then base class). I looked into ClassMetadataFactory that adds the fields to the classmetadata and figured if we could move the following $this->addInheritedFields($class, $parent); $this->addInheritedRelations($class, $parent); to after the point where the local fields are added to the classmetadata, this problem is solved. It might be even better if we have an annotation to specify the Ordering of columns but nevertheless this will help in cases where the base class columns appear after the derived class columns. Note: Did look into columnDefinition annotation to specify a "AFTER <column>", but that cannot be used during CREATE TABLE, only for ALTER TABLE and that too, its mysql specific.
admin added the Improvement label 2026-01-22 14:37:39 +01:00
Author
Owner

@doctrinebot commented on GitHub (Mar 5, 2015):

Comment created by topor-grabowski:

This would be a great feature. I am using inheritance in the entity classes and have the same problem.
Even if this is not a common taks for ORM, but i simplifies your life;-)

@doctrinebot commented on GitHub (Mar 5, 2015): Comment created by topor-grabowski: This would be a great feature. I am using inheritance in the entity classes and have the same problem. Even if this is not a common taks for ORM, but i simplifies your life;-)
Author
Owner

@romainbriand commented on GitHub (Apr 20, 2020):

We can live without it of course but it would be a nice feature to have 👍

@romainbriand commented on GitHub (Apr 20, 2020): We can live without it of course but it would be a nice feature to have 👍
Author
Owner

@itsjavi commented on GitHub (Feb 17, 2022):

I have the same problem when using traits or inheritance: the child class properties are put before the ones used in the use trait statements (which I expect to go first). My app shows the user the list of columns in a UI, so it's weird to see other columns before the id for example.

I'm using the latest version of DBAL and the ORM 2.11.1

@itsjavi commented on GitHub (Feb 17, 2022): I have the same problem when using traits or inheritance: the child class properties are put before the ones used in the use trait statements (which I expect to go first). My app shows the user the list of columns in a UI, so it's weird to see other columns before the `id` for example. I'm using the latest version of DBAL and the ORM 2.11.1
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#4224