DDC-549: \Doctrine\ORM\Mapping\DatabaseDriver::loadMetadataForClass fails to detect primary key when primary key is also a foreign key #678

Open
opened 2026-01-22 12:46:32 +01:00 by admin · 0 comments
Owner

Originally created by @doctrinebot on GitHub (Apr 27, 2010).

Originally assigned to: @jwage on GitHub.

Jira issue originally created by user mjh_ca:

Encoutered this error while attempting to reverse engineer a database. Was previously working with 2.0-ALPHA4 so this is the result of some change between ALPHA4 and BETA1.

The reverse engineering process fails with a Doctrine\ORM\Mapping\MappingException with message "No identifier/primary key specified for Entity mytable. Every entity must have an identifier/primary key."

This exception is thrown because the "identifier" property of the ClassMetadataInfo object passed to loadMetadataForClass is not populated with the expected value (which in this case should be array('a_id', 'b_id')

It appears that this is caused by the following code in \Doctrine\ORM\Mapping\DatabaseDriver::loadMetadataForClass():

            // Skip columns that are foreign keys
            foreach ($foreignKeys as $foreignKey) {
                if (in*array(strtolower($column->getName()), array*map('strtolower', $foreignKey->getColumns()))) {
                    continue(2);
                }
            }

The table definition is something like:

CREATE TABLE a (
  a*id BIGINT UNSIGNED NOT NULL AUTO*INCREMENT,
  PRIMARY KEY (a_id)
) ENGINE=InnoDB;

CREATE TABLE b (
  b*id BIGINT UNSIGNED NOT NULL AUTO*INCREMENT,
  PRIMARY KEY (b_id)
) ENGINE=InnoDB;

/** this is the table that fails to be reverse engineered **/
CREATE TABLE mytable (
  a_id BIGINT UNSIGNED NOT NULL,
  b_id BIGINT UNSIGNED NOT NULL,
  PRIMARY KEY (a*id, b*id),
  CONSTRAINT `a*id_ibfk` FOREIGN KEY (`a_id`) REFERENCES `a` (`a*id`),
  CONSTRAINT `b*id_ibfk` FOREIGN KEY (`b_id`) REFERENCES `b` (`b*id`),
) ENGINE=InnoDB;
Originally created by @doctrinebot on GitHub (Apr 27, 2010). Originally assigned to: @jwage on GitHub. Jira issue originally created by user mjh_ca: Encoutered this error while attempting to reverse engineer a database. Was previously working with 2.0-ALPHA4 so this is the result of some change between ALPHA4 and BETA1. The reverse engineering process fails with a Doctrine\ORM\Mapping\MappingException with message "No identifier/primary key specified for Entity `mytable`. Every entity must have an identifier/primary key." This exception is thrown because the "identifier" property of the ClassMetadataInfo object passed to loadMetadataForClass is not populated with the expected value (which in this case should be array('a_id', 'b_id') It appears that this is caused by the following code in \Doctrine\ORM\Mapping\DatabaseDriver::loadMetadataForClass(): ``` // Skip columns that are foreign keys foreach ($foreignKeys as $foreignKey) { if (in*array(strtolower($column->getName()), array*map('strtolower', $foreignKey->getColumns()))) { continue(2); } } ``` The table definition is something like: ``` CREATE TABLE a ( a*id BIGINT UNSIGNED NOT NULL AUTO*INCREMENT, PRIMARY KEY (a_id) ) ENGINE=InnoDB; CREATE TABLE b ( b*id BIGINT UNSIGNED NOT NULL AUTO*INCREMENT, PRIMARY KEY (b_id) ) ENGINE=InnoDB; /** this is the table that fails to be reverse engineered **/ CREATE TABLE mytable ( a_id BIGINT UNSIGNED NOT NULL, b_id BIGINT UNSIGNED NOT NULL, PRIMARY KEY (a*id, b*id), CONSTRAINT `a*id_ibfk` FOREIGN KEY (`a_id`) REFERENCES `a` (`a*id`), CONSTRAINT `b*id_ibfk` FOREIGN KEY (`b_id`) REFERENCES `b` (`b*id`), ) ENGINE=InnoDB; ```
admin added the Bug label 2026-01-22 12:46:32 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#678