Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-732

Class Table Inheritance - wrong table order on insert with more than one level of inheritance

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 2.0-BETA3
    • Fix Version/s: 2.0-RC1
    • Component/s: ORM
    • Security Level: All
    • Labels:
      None

      Description

      This is the error that i get when i try to create new entity Employee

      $newEmployee = new Users_Entity_Model_Employee;
      //here i set the data
      .....
      
      $em->persist($newEmployee);
      $em->flush();
      

      SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`crm/Employees`, CONSTRAINT `employee_person` FOREIGN KEY (`contactID`) REFERENCES `Persons` (`contactID`) ON DELETE NO ACTION ON UPDATE NO ACTION)

      /**
       *
       * @Entity
       * @InheritanceType("JOINED")
       * @Table(name="Contacts")
       * @DiscriminatorColumn(name="contactType", type="string")
       * @DiscriminatorMap({"PERSON" = "Users_Model_Entity_Person", "COMPANY" = "Users_Model_Entity_Company", "EMPLOYEE" = "Users_Model_Entity_Employee", "FIRM" = "Users_Model_Entity_Firm"})
       */
      class Users_Model_Entity_Contacts 
      {
      
      
      	/**
      	 *  @Id  @Column(name="contactID", type="integer")
      	 *  @GeneratedValue(strategy="AUTO")
      	 *
      	 */
      	protected $contactID;
      
      	// ...
      
      }
      
      /**
       *
       * @Table(name="Persons")
       * @entity(repositoryClass="Users_Model_PersonRepo")
      
      
       */
      class Users_Model_Entity_Person extends Users_Model_Entity_Contacts
      {
       // ...
      }
      
      
      
      
      /**
       *
       * @Table(name="Employees")
       * @entity
       */
      class Users_Model_Entity_Employee extends Users_Model_Entity_Person
      {
      	// .....
      }
      
      1. DDC732Test.php
        4 kB
        victor Velkov
      2. testDoc.sql
        3 kB
        victor Velkov

        Activity

        Hide
        victor Velkov added a comment -

        Hi there,

        I will try with the sql generated by Doctrine. But the problem I got was when I have more than 2 levels of the inheritance hierarchy. When try to make an insert trough the lowest level (in my example the DDC732Driver. Actually it was not working with the DDC732Employee entity as well) the order of inserting was not correct. First it went trough the correct entity (in my example the DDC732Contacts), but then instead of going through DDC732Person it try to go trough the last level (DDC732Driver or DDC732Employee depending on the example). And then the MySql said that it can not go trough. Also maybe the reason is that I am using InnoDB, but then it shouldn't really matter because I thought that the hierarchy inheritance is supposed to work with it. Actually I am almost 100% sure the problem is in the InnoDB could you please try with it. If it is still working for you i will reexamine my application and find the mistake.

        Thanks in advance for the help and sorry. Now on second thought I should have explained all that in the beginning.

        Show
        victor Velkov added a comment - Hi there, I will try with the sql generated by Doctrine. But the problem I got was when I have more than 2 levels of the inheritance hierarchy. When try to make an insert trough the lowest level (in my example the DDC732Driver. Actually it was not working with the DDC732Employee entity as well) the order of inserting was not correct. First it went trough the correct entity (in my example the DDC732Contacts), but then instead of going through DDC732Person it try to go trough the last level (DDC732Driver or DDC732Employee depending on the example). And then the MySql said that it can not go trough. Also maybe the reason is that I am using InnoDB, but then it shouldn't really matter because I thought that the hierarchy inheritance is supposed to work with it. Actually I am almost 100% sure the problem is in the InnoDB could you please try with it. If it is still working for you i will reexamine my application and find the mistake. Thanks in advance for the help and sorry. Now on second thought I should have explained all that in the beginning.
        Hide
        Benjamin Eberlei added a comment -

        Doctrine2 only creates InnoDB tables. Are you using foreign keys in a different way?

        Show
        Benjamin Eberlei added a comment - Doctrine2 only creates InnoDB tables. Are you using foreign keys in a different way?
        Hide
        Benjamin Eberlei added a comment -

        Closed because we can't reproduce this. Schema of the user differs from the ORM SchemaTool and causes this issue.

        Show
        Benjamin Eberlei added a comment - Closed because we can't reproduce this. Schema of the user differs from the ORM SchemaTool and causes this issue.
        Hide
        victor Velkov added a comment -

        Hi there i am reopening this ticket, because i found problem with the logic of creating inherited join tables from your create schema tool. I was expecting when we have more than one level of inherited tables the foreign key constrain to go to the parent and not to the root table. And because of that with the sql generated from your tool it was ok and it was not working with my sql. I would like to know are you going to fix that or it was intended to work that way from the beginning? Because logically for me and my colleagues if Employee extends Person then the foreign key constrain in Employee should go to Person and at the same time Person extends Contact then foreign key constrain in Person should go to Contact. And with your schema tool in that instance the foreign key constrain in Employee go to Contact which is wrong.

        Show
        victor Velkov added a comment - Hi there i am reopening this ticket, because i found problem with the logic of creating inherited join tables from your create schema tool. I was expecting when we have more than one level of inherited tables the foreign key constrain to go to the parent and not to the root table. And because of that with the sql generated from your tool it was ok and it was not working with my sql. I would like to know are you going to fix that or it was intended to work that way from the beginning? Because logically for me and my colleagues if Employee extends Person then the foreign key constrain in Employee should go to Person and at the same time Person extends Contact then foreign key constrain in Person should go to Contact. And with your schema tool in that instance the foreign key constrain in Employee go to Contact which is wrong.
        Hide
        Benjamin Eberlei added a comment -

        This is a documentation issue.

        I have now documented the SQL schema requirements for inheritance scenarios.

        Closing this issue.

        Show
        Benjamin Eberlei added a comment - This is a documentation issue. I have now documented the SQL schema requirements for inheritance scenarios. Closing this issue.

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            victor Velkov
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: