Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine OneToOne incorrectly? generating a UNIQUE INDEX

SchemaTool is generating unique index for associations that are OneToOne. I believe this is incorrect.

Section 6.6 of the Associations manual page at Doctrine shows an example of a OneToOne for a Product has one Shipping. This is shown to generate the Product table:

CREATE TABLE Product (     id INT AUTO_INCREMENT NOT NULL,     shipping_id INT DEFAULT NULL,     PRIMARY KEY(id) ) ENGINE = InnoDB; 

However, with the same code for my entity User has one Organisation, my User table SQL is generated as

CREATE TABLE User (     id INT AUTO_INCREMENT NOT NULL,     organisation_id INT DEFAULT NULL,     UNIQ_3B978F9FA7F43455 (organisation_id),     PRIMARY KEY(id) ) ENGINE = InnoDB; 

This prevents me adding 2 users with the same Organisation. Not correct.

I additinally tried to be verbose with the unique JoinColumn annotation param.

@JoinColumn(name="organisation_id", referencedColumnName="id", unique="false") 

Any ideas? I can't seem to find anything at all about this.

Thanks

like image 493
PorridgeBear Avatar asked Nov 20 '11 23:11

PorridgeBear


1 Answers

If One organisation has Many Users and Many Users have One and only one organisation then it's not a One-to-One association.

One-to-One associations have to be unique.

Your association is ManyToOne on the User side and OneToMany on the organisation side.

User.php

/**  * @ManyToOne(targetEntity="Organisation")  */ private $organisation; 

Organisation.php

use Doctrine\Common\Collections\ArrayCollection;  /**  * @OneToMany(targetEntity="User", mappedBy="organisation")  */ private $users;  function __construct() {     $this->users = new ArrayCollection(); } 
like image 58
Kasheen Avatar answered Sep 30 '22 08:09

Kasheen