Skip to content

Loading…

DDC-1587: Foreign key as Primary does not work as intended #2223

Closed
doctrinebot opened this Issue · 11 comments

2 participants

@doctrinebot

Jira issue originally created by user presteus:

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.

/****
 * @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;

    [...]

}

/****
 * @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;

    [...]

}

@doctrinebot

Comment created by presteus:

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.

@doctrinebot

Comment created by presteus:

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

  '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?
@doctrinebot

Comment created by presteus:

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_idfor FK.

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

@doctrinebot

Comment created by @guilhermeblanco:

Hi,

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

Regards,

@doctrinebot

Comment created by @beberlei:

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.

@doctrinebot

Comment created by presteus:

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.

@doctrinebot

Comment created by presteus:

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

@doctrinebot

Comment created by presteus:

Basic Agent and Identifier entities.

@doctrinebot

Comment created by @guilhermeblanco:

Hi,

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

Commit reference: 0f07044

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

@doctrinebot

Issue was closed with resolution "Invalid"

@doctrinebot doctrinebot closed this
@doctrinebot doctrinebot added the Bug label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.