Incorrect order of alters in doctrine:schema:update --force #6118

Open
opened 2026-01-22 15:27:03 +01:00 by admin · 3 comments
Owner

Originally created by @4n70w4 on GitHub (Dec 5, 2018).

Bug Report

Summary

I changed the yml files for my 2 entityes. I understand now alters occur in alphabetical order?

Current behavior

Then executed:

php bin/console doctrine:generate:entities MyBundle --no-backup

Then try to update db schema:

php bin/console doctrine:schema:update --force

But I get an error:

 Updating database schema...

In AbstractMySQLDriver.php line 126:

  An exception occurred while executing 'ALTER TABLE bonus ADD CONSTRAINT FK_F7569EF741DEE7B9 FOREIGN KEY (user_id) REFERENCES user (id)':

  SQLSTATE[HY000]: General error: 3734 Failed to add the foreign key constraint. Missing column 'id' for constraint 'FK_F7569EF741DEE7B9' in the referenced table 'user'

In PDOConnection.php line 109:

  SQLSTATE[HY000]: General error: 3734 Failed to add the foreign key constraint. Missing column 'id' for constraint 'FK_F7569EF741DEE7B9' in the referenced table 'user'

In PDOConnection.php line 107:

  SQLSTATE[HY000]: General error: 3734 Failed to add the foreign key constraint. Missing column 'id' for constraint 'FK_F7569EF741DEE7B9' in the referenced table 'user'

Because column id in table user need create before create relation from bonus to user.

How to reproduce

Initial only User entity:

MyBundle\Entity\User:
    type: entity
    table: null
    repositoryClass: MyBundle\Repository\UserRepository
    id:
        token:
            type: string
            length: '128'
            unique: true
    fields:
        email:
            type: string
            length: '80'

    lifecycleCallbacks: {  }

php bin/console doctrine:generate:entities MyBundle --no-backup

php bin/console doctrine:schema:update --force

In next version add Bonus entity and change User entity:

MyBundle\Entity\Bonus:
  type: entity
  table: null
  repositoryClass: MyBundle\Repository\BonusRepository
  id:
    id:
      type: integer
      id: true
      generator:
        strategy: AUTO
  manyToOne:
    user:
      targetEntity: MyBundle\Entity\User
  fields:
    value:
      type: decimal
      precision: '14'
      scale: '8'

  lifecycleCallbacks: {  }

MyBundle\Entity\User:
    type: entity
    table: null
    repositoryClass: MyBundle\Repository\UserRepository
    id:
        id:
            type: integer
            nullable: true
            unique: true
            id: true
            generator:
                strategy: AUTO
    fields:
        token:
            type: string
            length: '128'
            unique: true
        email:
            type: string
            length: '80'

    lifecycleCallbacks: {  }

php bin/console doctrine:generate:entities MyBundle --no-backup

php bin/console doctrine:schema:update --force

Expected behavior

I expect the execution of alters in the correct order.

doctrine/annotations                  v1.6.0            Docblock Annotations Parser
doctrine/cache                        v1.8.0            Caching library offering an object-oriented API for many cache backends
doctrine/collections                  v1.5.0            Collections Abstraction library
doctrine/common                       v2.9.0            Common Library for Doctrine projects
doctrine/dbal                         v2.8.0            Database Abstraction Layer
doctrine/doctrine-bundle              1.9.1             Symfony DoctrineBundle
doctrine/doctrine-cache-bundle        1.3.5             Symfony Bundle for Doctrine Cache
doctrine/event-manager                v1.0.0            Doctrine Event Manager component
doctrine/inflector                    v1.3.0            Common String Manipulations with regard to casing and singular/plural rules.
doctrine/instantiator                 1.1.0             A small, lightweight utility to instantiate objects in PHP without invoking their constructors
doctrine/lexer                        v1.0.1            Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers.
doctrine/orm                          v2.6.2            Object-Relational-Mapper for PHP
doctrine/persistence                  v1.0.1            Doctrine Persistence abstractions.
doctrine/reflection                   v1.0.0            Doctrine Reflection component
Originally created by @4n70w4 on GitHub (Dec 5, 2018). ### Bug Report #### Summary I changed the yml files for my 2 entityes. I understand now alters occur in alphabetical order? #### Current behavior Then executed: `php bin/console doctrine:generate:entities MyBundle --no-backup` Then try to update db schema: `php bin/console doctrine:schema:update --force` But I get an error: ``` Updating database schema... In AbstractMySQLDriver.php line 126: An exception occurred while executing 'ALTER TABLE bonus ADD CONSTRAINT FK_F7569EF741DEE7B9 FOREIGN KEY (user_id) REFERENCES user (id)': SQLSTATE[HY000]: General error: 3734 Failed to add the foreign key constraint. Missing column 'id' for constraint 'FK_F7569EF741DEE7B9' in the referenced table 'user' In PDOConnection.php line 109: SQLSTATE[HY000]: General error: 3734 Failed to add the foreign key constraint. Missing column 'id' for constraint 'FK_F7569EF741DEE7B9' in the referenced table 'user' In PDOConnection.php line 107: SQLSTATE[HY000]: General error: 3734 Failed to add the foreign key constraint. Missing column 'id' for constraint 'FK_F7569EF741DEE7B9' in the referenced table 'user' ``` Because column id in table user need create before create relation from bonus to user. #### How to reproduce Initial only User entity: ``` MyBundle\Entity\User: type: entity table: null repositoryClass: MyBundle\Repository\UserRepository id: token: type: string length: '128' unique: true fields: email: type: string length: '80' lifecycleCallbacks: { } ``` `php bin/console doctrine:generate:entities MyBundle --no-backup` `php bin/console doctrine:schema:update --force` In next version add Bonus entity and change User entity: ``` MyBundle\Entity\Bonus: type: entity table: null repositoryClass: MyBundle\Repository\BonusRepository id: id: type: integer id: true generator: strategy: AUTO manyToOne: user: targetEntity: MyBundle\Entity\User fields: value: type: decimal precision: '14' scale: '8' lifecycleCallbacks: { } ``` ``` MyBundle\Entity\User: type: entity table: null repositoryClass: MyBundle\Repository\UserRepository id: id: type: integer nullable: true unique: true id: true generator: strategy: AUTO fields: token: type: string length: '128' unique: true email: type: string length: '80' lifecycleCallbacks: { } ``` `php bin/console doctrine:generate:entities MyBundle --no-backup` `php bin/console doctrine:schema:update --force` #### Expected behavior I expect the execution of alters in the correct order. ``` doctrine/annotations v1.6.0 Docblock Annotations Parser doctrine/cache v1.8.0 Caching library offering an object-oriented API for many cache backends doctrine/collections v1.5.0 Collections Abstraction library doctrine/common v2.9.0 Common Library for Doctrine projects doctrine/dbal v2.8.0 Database Abstraction Layer doctrine/doctrine-bundle 1.9.1 Symfony DoctrineBundle doctrine/doctrine-cache-bundle 1.3.5 Symfony Bundle for Doctrine Cache doctrine/event-manager v1.0.0 Doctrine Event Manager component doctrine/inflector v1.3.0 Common String Manipulations with regard to casing and singular/plural rules. doctrine/instantiator 1.1.0 A small, lightweight utility to instantiate objects in PHP without invoking their constructors doctrine/lexer v1.0.1 Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers. doctrine/orm v2.6.2 Object-Relational-Mapper for PHP doctrine/persistence v1.0.1 Doctrine Persistence abstractions. doctrine/reflection v1.0.0 Doctrine Reflection component ```
Author
Owner

@4n70w4 commented on GitHub (Dec 5, 2018):

Also in this case command generate incorrect alter for user.id:

ALTER TABLE user ADD id INT AUTO_INCREMENT NOT NULL;

And got error:

Error Code: 1075. Incorrect table definition; there can be only one auto column and it must be defined as a key

Correct alter like this:

ALTER TABLE user ADD id INT AUTO_INCREMENT NOT NULL, ADD PRIMARY KEY (id);

@4n70w4 commented on GitHub (Dec 5, 2018): Also in this case command generate incorrect alter for user.id: `ALTER TABLE user ADD id INT AUTO_INCREMENT NOT NULL;` And got error: `Error Code: 1075. Incorrect table definition; there can be only one auto column and it must be defined as a key` Correct alter like this: `ALTER TABLE user ADD id INT AUTO_INCREMENT NOT NULL, ADD PRIMARY KEY (`id`);`
Author
Owner

@Ocramius commented on GitHub (Dec 5, 2018):

@4n70w4 can you please bump the dependencies and try again? Just need to make sure latest stable is affected as well.

@Ocramius commented on GitHub (Dec 5, 2018): @4n70w4 can you please bump the dependencies and try again? Just need to make sure latest stable is affected as well.
Author
Owner

@4n70w4 commented on GitHub (Dec 5, 2018):

Ok, updated to:

doctrine/annotations                  v1.6.0            Docblock Annotations Parser
doctrine/cache                        v1.8.0            Caching library offering an object-oriented API for many cache backends
doctrine/collections                  v1.5.0            Collections Abstraction library
doctrine/common                       v2.10.0           PHP Doctrine Common project is a library that provides additional functionality that other Doctrine projects depend on such as better refl...
doctrine/dbal                         v2.9.0            Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.
doctrine/doctrine-bundle              1.10.0            Symfony DoctrineBundle
doctrine/doctrine-cache-bundle        1.3.5             Symfony Bundle for Doctrine Cache
doctrine/event-manager                v1.0.0            Doctrine Event Manager component
doctrine/inflector                    v1.3.0            Common String Manipulations with regard to casing and singular/plural rules.
doctrine/instantiator                 1.1.0             A small, lightweight utility to instantiate objects in PHP without invoking their constructors
doctrine/lexer                        v1.0.1            Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers.
doctrine/orm                          v2.6.3            Object-Relational-Mapper for PHP
doctrine/persistence                  v1.1.0            The Doctrine Persistence project is a set of shared interfaces and functionality that the different Doctrine object mappers share.
doctrine/reflection                   v1.0.0            Doctrine Reflection component

Problem with Error Code: 1075. Incorrect table definition; there can be only one auto column and it must be defined as a key solved.

But case from topic not solved.

php bin/console doctrine:schema:update --force --dump-sql

The following SQL statements will be executed:

     CREATE TABLE bonus (id INT AUTO_INCREMENT NOT NULL, user_id INT DEFAULT NULL, value NUMERIC(14, 8) NOT NULL, INDEX IDX_F7569EF741DEE7B9 (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET UTF8 COLLATE UTF8_unicode_ci ENGINE = InnoDB;
     ALTER TABLE bonus ADD CONSTRAINT FK_F7569EF741DEE7B9 FOREIGN KEY (user_id) REFERENCES user (id);
     ALTER TABLE user ADD id INT AUTO_INCREMENT NOT NULL, ADD PRIMARY KEY (id);

Alter with constraint before alter with foreign field.

@4n70w4 commented on GitHub (Dec 5, 2018): Ok, updated to: ``` doctrine/annotations v1.6.0 Docblock Annotations Parser doctrine/cache v1.8.0 Caching library offering an object-oriented API for many cache backends doctrine/collections v1.5.0 Collections Abstraction library doctrine/common v2.10.0 PHP Doctrine Common project is a library that provides additional functionality that other Doctrine projects depend on such as better refl... doctrine/dbal v2.9.0 Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management. doctrine/doctrine-bundle 1.10.0 Symfony DoctrineBundle doctrine/doctrine-cache-bundle 1.3.5 Symfony Bundle for Doctrine Cache doctrine/event-manager v1.0.0 Doctrine Event Manager component doctrine/inflector v1.3.0 Common String Manipulations with regard to casing and singular/plural rules. doctrine/instantiator 1.1.0 A small, lightweight utility to instantiate objects in PHP without invoking their constructors doctrine/lexer v1.0.1 Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers. doctrine/orm v2.6.3 Object-Relational-Mapper for PHP doctrine/persistence v1.1.0 The Doctrine Persistence project is a set of shared interfaces and functionality that the different Doctrine object mappers share. doctrine/reflection v1.0.0 Doctrine Reflection component ``` Problem with `Error Code: 1075. Incorrect table definition; there can be only one auto column and it must be defined as a key` solved. But case from topic not solved. `php bin/console doctrine:schema:update --force --dump-sql` The following SQL statements will be executed: ``` CREATE TABLE bonus (id INT AUTO_INCREMENT NOT NULL, user_id INT DEFAULT NULL, value NUMERIC(14, 8) NOT NULL, INDEX IDX_F7569EF741DEE7B9 (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET UTF8 COLLATE UTF8_unicode_ci ENGINE = InnoDB; ALTER TABLE bonus ADD CONSTRAINT FK_F7569EF741DEE7B9 FOREIGN KEY (user_id) REFERENCES user (id); ALTER TABLE user ADD id INT AUTO_INCREMENT NOT NULL, ADD PRIMARY KEY (id); ``` Alter with constraint before alter with foreign field.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6118