OneToOne 学说不正确?生成唯一索引 [英] Doctrine OneToOne incorrectly? generating a UNIQUE INDEX

查看:21
本文介绍了OneToOne 学说不正确?生成唯一索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SchemaTool 正在为 OneToOne 关联生成唯一索引.我相信这是不正确的.

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

Doctrine 的 Associations 手册页的第 6.6 节显示了一个产品的 OneToOne 示例,该产品具有一个 Shipping.这显示生成产品表:

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;

但是,对于我的实体用户有一个组织的相同代码,我的用户表 SQL 生成为

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;

这会阻止我添加 2 个具有相同组织的用户.不正确.

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

我还尝试使用独特的 JoinColumn 注释参数来详细说明.

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.

谢谢

推荐答案

如果一个组织有很多用户,而很多用户只有一个组织,那么这不是一对一的关联.

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.

您的关联在用户端是 ManyToOne,在组织端是 OneToMany.

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

用户.php

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

组织.php

use DoctrineCommonCollectionsArrayCollection;

/**
 * @OneToMany(targetEntity="User", mappedBy="organisation")
 */
private $users;

function __construct() {
    $this->users = new ArrayCollection();
}

这篇关于OneToOne 学说不正确?生成唯一索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆