Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8207885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:08:52+00:00 2026-06-07T09:08:52+00:00

I was recently reading the Doctrine 2 ‘s best practices and was stopped by

  • 0

I was recently reading the Doctrine 2‘s best practices and was stopped by this:

25.3. Avoid composite keys
Even though Doctrine fully supports composite keys it is best not to use them if possible. Composite keys
require additional work by Doctrine and thus have a higher probability
of errors.

What I don’t get is this: if "Doctrine fully supports composite keys", how can it make mistakes when dealing with such keys?

Therefore I would like to ask this question as a community wiki, hoping it will help us understand what the "probability of errors" are:

Do you have examples to share showing situations you’ve ran into whereby Doctrine 2 was not handling composite keys well/as it should?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T09:08:55+00:00Added an answer on June 7, 2026 at 9:08 am

    For my part I believe I’ve already ran into some issues with a very basic database schema containing a table which primary key is composed of 2 foreign keys:

    enter image description here

    When I generate my models, the entity corresponding to the product_i18n entity isn’t created:

    $ php doctrine-module.php orm:convert-mapping --namespace="Dbi\Entity\\" \
    --from-database --force annotation module/Dbi/src/
    Processing entity "Dbi\Entity\Locale"
    Processing entity "Dbi\Entity\Product"
    
    $ php doctrine-module.php orm:generate-entities --generate-annotations=1 \
    module/Dbi/src
    Processing entity "Dbi\Entity\Locale"
    Processing entity "Dbi\Entity\Product"
    

    Yet, I believe my database schema was properly created:

      PRIMARY KEY (`product_id`, `locale_id`) ,
      INDEX `fk_product_i18n_locale` (`locale_id` ASC) ,
      INDEX `fk_product_i18n_product` (`product_id` ASC) ,
      CONSTRAINT `fk_product_i18n_locale`
        FOREIGN KEY (`locale_id` )
        REFERENCES `mydb`.`locale` (`id` ),
      CONSTRAINT `fk_product_i18n_product`
        FOREIGN KEY (`product_id` )
        REFERENCES `mydb`.`product` (`id` )
    

    Also, Doctrine 2‘s Schema Manager shows that Doctrine seems to understand that relation perfectly (var_dump output altered to make it more concise: removing things such as array, RECURSION, string…):

    $em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
    $sm = $em->getConnection()->getSchemaManager();
    var_dump($sm->->listTables());
    
    object(Doctrine\DBAL\Schema\Table)#290 (10) {
    ["_name":protected]=> "product_i18n"
    
    ["_indexes":protected]=>
      ["primary"]=>
        ["_columns":protected]=>
          [0]=> "product_id"
          [1]=> "locale_id"
    
    ["_fkConstraints":protected]=>
      ["fk_product_i18n_locale"]=>
      object(Doctrine\DBAL\Schema\ForeignKeyConstraint)#285 (9) {
        ["_localColumnNames":protected]=> "locale_id"
        ["_foreignTableName":protected]=> "locale"
        ["_foreignColumnNames":protected]=> "id"
    
      ["fk_product_i18n_product"]=>
      object(Doctrine\DBAL\Schema\ForeignKeyConstraint)#286 (9) {
        ["_localColumnNames":protected]=> "product_id"
        ["_foreignTableName":protected]=> "product"
        ["_foreignColumnNames":protected]=> "id"
    

    Therefore I’m in a situation whereby I can query the schema for the product_i18n table, but can’t interact with that table because the corresponding entity model wasn’t generated.

    UPDATE: as user1136666 pointed out: the known issues and limitations page odes state the following:

    Although we state that we support composite primary keys that does not
    currently include foreign keys as primary key columns.

    The workaround is to define a surrogate key and add a unique constraint on the foreign keys, like so:

    CREATE  TABLE IF NOT EXISTS `mydb`.`product_i18n` (
      `id` INT NOT NULL AUTO_INCREMENT ,
      `name` VARCHAR(45) NULL ,
      `description` TEXT NULL ,
      `created_at` DATETIME NULL ,
      `modified_at` DATETIME NULL ,
      `product_id` INT NOT NULL ,
      `locale_id` INT NOT NULL ,
      INDEX `fk_product_i18n_locale` (`locale_id` ASC) ,
      INDEX `fk_product_i18n_product` (`product_id` ASC) ,
      PRIMARY KEY (`id`) ,
      UNIQUE (`locale_id` , `product_id`),
      CONSTRAINT `fk_product_i18n_locale`
        FOREIGN KEY (`locale_id` )
        REFERENCES `mydb`.`locale` (`id` )
      CONSTRAINT `fk_product_i18n_product`
        FOREIGN KEY (`product_id` )
        REFERENCES `mydb`.`product` (`id` )
    ENGINE = InnoDB;
    

    Feature request: http://www.doctrine-project.org/jira/browse/DDC-1926

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When reading about JQuery best practices, I read this recently: Never include Javascript events
I was recently reading this thread , on some of the worst PHP practices.
I was recently reading this discussion at SO where somebody commented that not all
I was reading a forum recently, and saw this comment: So, you see you've
I was recently reading this article by Dave Detlefs in which he presents a
I was recently reading a question here (some time this week), which I can't
I recently came across the subject of exact real arithmetic after reading this paper
I was recently reading this document which lists a number of strategies that could
I've recently begun exploring ORM tools such as Doctrine, and in my reading I'm
I was recently reading an article on the Windows Metafile vulnerability (http://en.wikipedia.org/wiki/Windows_Metafile_vulnerability#Third-party_patch) and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.