Schema update does not add column to the right place nor reorder them #6533

Closed
opened 2026-01-22 15:34:37 +01:00 by admin · 5 comments
Owner

Originally created by @mvorisek on GitHub (Sep 2, 2020).

Step to reproduce:

  1. create model like:
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 **/
class Record {
    /**
     * @var int
     * @ORM\Id
     * @ORM\Column(name="id", type="bigint", options={"unsigned"=true})
     **/
    public $id;

    /** @var \DateTime @ORM\Column(type="datetime_immutable") */
    public $createdAt;
}
  1. create DB, ie. run
vendor\doctrine\orm\bin\doctrine.php orm:schema-tool:update --dump-sql --complete --force
  1. then update the model by appending this right below the ID column:
    /** @var string @ORM\Column(type="string", length=500) */
    public $name;
  1. update the DB, ie. run:
vendor\doctrine\orm\bin\doctrine.php orm:schema-tool:update --dump-sql --complete --force
  1. notice, that the column was added at the end of the table, instead of using AFTER (see https://dev.mysql.com/doc/refman/5.7/en/alter-table.html#alter-table-redefine-column) in the update command (in step 4)

I belive this needs to be addressed in the Doctrine\DBAL\Schema\Comparator as it is used for diffing the schema in:
ccae8f7176/lib/Doctrine/ORM/Tools/SchemaTool.php (L906)

Originally created by @mvorisek on GitHub (Sep 2, 2020). Step to reproduce: 1. create model like: ``` use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity **/ class Record { /** * @var int * @ORM\Id * @ORM\Column(name="id", type="bigint", options={"unsigned"=true}) **/ public $id; /** @var \DateTime @ORM\Column(type="datetime_immutable") */ public $createdAt; } ``` 2. create DB, ie. run ``` vendor\doctrine\orm\bin\doctrine.php orm:schema-tool:update --dump-sql --complete --force ``` 3. then update the model by appending this right below the ID column: ``` /** @var string @ORM\Column(type="string", length=500) */ public $name; ``` 4. update the DB, ie. run: ``` vendor\doctrine\orm\bin\doctrine.php orm:schema-tool:update --dump-sql --complete --force ``` 5. notice, that the column was added at the end of the table, instead of using `AFTER` (see https://dev.mysql.com/doc/refman/5.7/en/alter-table.html#alter-table-redefine-column) in the update command (in step 4) I belive this needs to be addressed in the `Doctrine\DBAL\Schema\Comparator` as it is used for diffing the schema in: https://github.com/doctrine/orm/blob/ccae8f717684dcd871b418444d7ca7155c677169/lib/Doctrine/ORM/Tools/SchemaTool.php#L906
admin closed this issue 2026-01-22 15:34:37 +01:00
Author
Owner

@mvorisek commented on GitHub (Sep 2, 2020):

related with https://github.com/doctrine/dbal/issues/1411

@mvorisek commented on GitHub (Sep 2, 2020): related with https://github.com/doctrine/dbal/issues/1411
Author
Owner

@SenseException commented on GitHub (Sep 2, 2020):

If such a feature will be developed, I wouldn't determine the positions of columns on the order of an entity's properties. The structure of the class itself shouldn't affect the resulting query at all. An optional attribute in the Column annotation would be one way for a new configuration. I don't know about a use case that would justify implementing and maintaining this feature for all supported databases (yet).

@SenseException commented on GitHub (Sep 2, 2020): If such a feature will be developed, I wouldn't determine the positions of columns on the order of an entity's properties. The structure of the class itself shouldn't affect the resulting query at all. An optional attribute in the `Column` annotation would be one way for a new configuration. I don't know about a use case that would justify implementing and maintaining this feature for all supported databases (yet).
Author
Owner

@mvorisek commented on GitHub (Sep 2, 2020):

I wouldn't determine the positions of columns on the order of an entity's properties

order of an entity's properties should define implicit order, it is the case now, only when DB is updated, the order may end up to be different as shown in the example

@mvorisek commented on GitHub (Sep 2, 2020): > I wouldn't determine the positions of columns on the order of an entity's properties order of an entity's properties should define implicit order, it is the case now, only when DB is updated, the order may end up to be different as shown in the example
Author
Owner

@SenseException commented on GitHub (Sep 3, 2020):

Which is why an explicit order shouldn't be determined by the properties. The implicit order is not a feature but just the way it was implemented. If a project is using coding styles, that expect changes in the order of properties (e.g. order by scopes), it shouldn't result in an Alter-statement for the database.

@SenseException commented on GitHub (Sep 3, 2020): Which is why an explicit order shouldn't be determined by the properties. The implicit order is not a feature but just the way it was implemented. If a project is using coding styles, that expect changes in the order of properties (e.g. order by scopes), it shouldn't result in an Alter-statement for the database.
Author
Owner

@beberlei commented on GitHub (Sep 13, 2020):

This is a cosmetic thing that you can add yourself if you want to, but its not something that we are going to add to Doctrine DBAL Schema support.

@beberlei commented on GitHub (Sep 13, 2020): This is a cosmetic thing that you can add yourself if you want to, but its not something that we are going to add to Doctrine DBAL Schema support.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6533