[DDC-1587] Foreign key as Primary does not work as intended Created: 08/Jan/12  Updated: 17/Jan/12  Resolved: 17/Jan/12

Status: Resolved
Project: Doctrine 2 - ORM
Component/s: ORM
Affects Version/s: 2.1.5
Fix Version/s: None
Security Level: All

Type: Bug Priority: Blocker
Reporter: Presteus Assignee: Guilherme Blanco
Resolution: Invalid Votes: 0
Labels: None

Attachments: File AgentDdc1587.php     File IdentifierDdc1587.php    

 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;

    [...]

}



 Comments   
Comment by Presteus [ 08/Jan/12 ]

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.

Comment by Presteus [ 08/Jan/12 ]

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?

Comment by Presteus [ 08/Jan/12 ]

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?

Comment by Guilherme Blanco [ 13/Jan/12 ]

Hi,

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

Regards,

Comment by Benjamin Eberlei [ 15/Jan/12 ]

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.

Comment by Presteus [ 15/Jan/12 ]

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.

Comment by Presteus [ 16/Jan/12 ]

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).

Comment by Presteus [ 16/Jan/12 ]

Basic Agent and Identifier entities.

Comment by Guilherme Blanco [ 17/Jan/12 ]

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.

Generated at Mon Sep 22 22:21:53 UTC 2014 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.