Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-1587

Foreign key as Primary does not work as intended

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Blocker Blocker
    • Resolution: Invalid
    • Affects Version/s: 2.1.5
    • Fix Version/s: None
    • Component/s: ORM
    • Security Level: All
    • Labels:
      None

      Description

      Hi,

      In first time, sorry for my bad english.

      The feature Foreign key as Primary does not work as intended.

      The CLI tool report an error on validating mapping:
      The referenced column name 'pk_agent' does not have a corresponding field with this column name on the class 'BddP\Model\Entity\Sd\Contact'.

      If I run the application, many features works: querying, entity population.
      But for persist a new Agent without associated entities for OneToOne; the ORM report :
      PDOException: SQLSTATE[23502]: Not null violation on "pk" column

      And If I comment all OneToOne, the persist work.

      Thank you in advance for your response.

      Agent.php
      /**
       * @Entity(repositoryClass="BddP\Model\Entity\Repository\Sd\Agent")
       * @Table(name="agent")
       */
      class Agent extends AEntity
      {
          /**
           * Agent ID
           * @var int
           *
           * @Id
           * @GeneratedValue
           * @Column(name="pk", type="integer", nullable=false)
           */
          private $pk;
          /**
           * @var BddP\Model\Entity\Sd\Contact
           *
           * @OneToOne(targetEntity="BddP\Model\Entity\Sd\Contact", cascade={"remove,merge,detach"}, mappedBy="agent")
           * @JoinColumn(name="pk", referencedColumnName="pk_agent")
           */
          private $contact;
      
          [...]
      
      }
      
      
      Contact.php
      /**
       * @Table(name="contact")
       */
      class Contact extends AEntity
      {
          /**
           * The agent
           * @var BddP\Model\Entity\Sd\Agent
           *
           * @Id
           * @OneToOne(targetEntity="BddP\Model\Entity\Sd\Agent", inversedBy="contact")
           * @JoinColumn(name="pk_agent", referencedColumnName="pk")
           */
          private $agent;
      
          [...]
      
      }
      
      
      1. IdentifierDdc1587.php
        3 kB
        Presteus
      2. AgentDdc1587.php
        3 kB
        Presteus

        Activity

        Hide
        Presteus added a comment -

        In addition,
        1. The generated SQL from CLI tool is correct.
        2. I think the problem is located when the associated entity request the Primary Key.

        Show
        Presteus added a comment - In addition, 1. The generated SQL from CLI tool is correct. 2. I think the problem is located when the associated entity request the Primary Key.
        Hide
        Presteus added a comment -

        After some dumping. I found a strange state :

        BasicEntityPersister : $this->_getInsertSQL() returns
        INSERT INTO agent (pk, name, firstname, birthday, email, address, city, postcode, phone, mobile) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

        And the PK parameter in $insertData has null value

        array
          'agent' => 
            array
              'pk' => null
              'name' => string 'XXX' (length=7)
              'firstname' => string 'YYY' (length=8)
              'birthday' => 
                object(DateTime)[283]
                  public 'date' => string '1970-01-01 00:00:00' (length=19)
                  public 'timezone_type' => int 3
                  public 'timezone' => string 'Europe/Paris' (length=12)
              'email' => string 'email@domain.tdl' (length=16)
              'address' => null
              'city' => null
              'postcode' => null
              'phone' => null
              'mobile' => null

        But the variable $idGen constains the right value...

        object(Doctrine\ORM\Id\SequenceGenerator)[335]
          private '_allocationSize' => int 1
          private '_sequenceName' => string 'agent_pk_seq' (length=12)
          private '_nextValue' => int 451
          private '_maxValue' => int 451

        What have I misunderstood?

        Show
        Presteus added a comment - After some dumping. I found a strange state : BasicEntityPersister : $this->_getInsertSQL() returns INSERT INTO agent (pk, name, firstname, birthday, email, address, city, postcode, phone, mobile) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) And the PK parameter in $insertData has null value array 'agent' => array 'pk' => null 'name' => string 'XXX' (length=7) 'firstname' => string 'YYY' (length=8) 'birthday' => object(DateTime)[283] public 'date' => string '1970-01-01 00:00:00' (length=19) public 'timezone_type' => int 3 public 'timezone' => string 'Europe/Paris' (length=12) 'email' => string 'email@domain.tdl' (length=16) 'address' => null 'city' => null 'postcode' => null 'phone' => null 'mobile' => null But the variable $idGen constains the right value... object(Doctrine\ORM\Id\SequenceGenerator)[335] private '_allocationSize' => int 1 private '_sequenceName' => string 'agent_pk_seq' (length=12) private '_nextValue' => int 451 private '_maxValue' => int 451 What have I misunderstood?
        Hide
        Presteus added a comment -

        After some test, other entity do not reproduce this error.

        So the only one reason for this bug: The mapping is incorrect.

        And I find... This annotation @JoinColumn(name="pk_agent", referencedColumnName="pk") add a foreign key on agent primary key.

        But in the documentation the default value are `id` for PK and `table_id`for FK.

        Then my question is still open: What have I misunderstood?

        Show
        Presteus added a comment - After some test, other entity do not reproduce this error. So the only one reason for this bug: The mapping is incorrect. And I find... This annotation @JoinColumn(name="pk_agent", referencedColumnName="pk") add a foreign key on agent primary key. But in the documentation the default value are `id` for PK and `table_id`for FK. Then my question is still open: What have I misunderstood?
        Hide
        Guilherme Blanco added a comment -

        Hi,

        Can you please try to wrap your issue into a test case?
        I could not reproduce here.

        Regards,

        Show
        Guilherme Blanco added a comment - Hi, Can you please try to wrap your issue into a test case? I could not reproduce here. Regards,
        Hide
        Benjamin Eberlei added a comment -

        First, schema validator had a bug that reported this errors in mapping although it was correct.

        Second, I think your mapping is wrong. In this case you cannot have a bidirectional OneToOne.

        Show
        Benjamin Eberlei added a comment - First, schema validator had a bug that reported this errors in mapping although it was correct. Second, I think your mapping is wrong. In this case you cannot have a bidirectional OneToOne.
        Hide
        Presteus added a comment -

        Thanks for our response and your time, especially Guilherme for my no response.

        I'm right with you Benjamin, it is my responsability to determinate wich field do not need to create a FK...

        This is the point of my misunderstood.

        Show
        Presteus added a comment - Thanks for our response and your time, especially Guilherme for my no response. I'm right with you Benjamin, it is my responsability to determinate wich field do not need to create a FK... This is the point of my misunderstood.
        Hide
        Presteus added a comment -

        Hi,

        I reopen this issue because the problem still alive, not totally the same but it's a side effect due to the removal of the @JoinColumn.

        Without the annotation my Agent do not retrieve the Identifier if and only if the Identifier have all fields with null value (except PK of course).
        With the annotation my Agent retrieve the Identifier but the valide-schema returns:

        [Mapping]  FAIL - The entity-class 'Entity\Agent' mapping is invalid:
        * The referenced column name 'pk_agent' does not have a corresponding field with this column name on the class 'Entity\Identifier'.
        

        And the dump for schema-tool returns

        ALTER TABLE agent ADD CONSTRAINT FK_268B9C9DB486D3D9 FOREIGN KEY (pk) REFERENCES identifier(pk_agent) NOT DEFERRABLE INITIALLY IMMEDIATE;
        

        For the moment, I do not trace inside the core of framework but i think the problem turn around the @JoinColum and custom name for primary key.

        If i do not use the join colum, how the framework retrieve pk name ?
        But if I provide the join column, Doctrine create a FK (without considering mapping error).

        Show
        Presteus added a comment - Hi, I reopen this issue because the problem still alive, not totally the same but it's a side effect due to the removal of the @JoinColumn. Without the annotation my Agent do not retrieve the Identifier if and only if the Identifier have all fields with null value (except PK of course). With the annotation my Agent retrieve the Identifier but the valide-schema returns: [Mapping] FAIL - The entity-class 'Entity\Agent' mapping is invalid: * The referenced column name 'pk_agent' does not have a corresponding field with this column name on the class 'Entity\Identifier'. And the dump for schema-tool returns ALTER TABLE agent ADD CONSTRAINT FK_268B9C9DB486D3D9 FOREIGN KEY (pk) REFERENCES identifier(pk_agent) NOT DEFERRABLE INITIALLY IMMEDIATE; For the moment, I do not trace inside the core of framework but i think the problem turn around the @JoinColum and custom name for primary key. If i do not use the join colum, how the framework retrieve pk name ? But if I provide the join column, Doctrine create a FK (without considering mapping error).
        Hide
        Presteus added a comment -

        Basic Agent and Identifier entities.

        Show
        Presteus added a comment - Basic Agent and Identifier entities.
        Hide
        Guilherme Blanco added a comment -

        Hi,

        I added coverage for your issue in master.
        It seems the issue have been addressed already. Are you able to upgrade?

        Commit reference: https://github.com/doctrine/doctrine2/commit/0f070448367f90dffb81225c680ef9b3490a3f0a

        @beberlei He's still able to make a bidirectional association, there's not restriction for that code-wise.

        Closing the issue as invalid, since recent versions have already addressed the issue.

        Show
        Guilherme Blanco added a comment - Hi, I added coverage for your issue in master. It seems the issue have been addressed already. Are you able to upgrade? Commit reference: https://github.com/doctrine/doctrine2/commit/0f070448367f90dffb81225c680ef9b3490a3f0a @beberlei He's still able to make a bidirectional association, there's not restriction for that code-wise. Closing the issue as invalid, since recent versions have already addressed the issue.

          People

          • Assignee:
            Guilherme Blanco
            Reporter:
            Presteus
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: