与Doctrine具体继承的外键关系

时间:2011-05-12 14:14:36

标签: inheritance symfony1 doctrine foreign-key-relationship

在我的架构中,我有一个通用表Animal和一个继承的表Dog。 在使用doctrine之前,我曾经使用继承的id实现此模式,该id将引用通用id作为外键。 我不能用Doctrine重现它,我觉得缺少了一些东西。

我以前制作的模式如下:

CREATE TABLE `animal` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `color` VARCHAR(20) NOT NULL,
  PRIMARY KEY (`id`)
);

CREATE TABLE `dog` (
  `id` INT UNSIGNED NOT NULL,
  `breed` VARCHAR(40) NOT NULL,
  KEY `id` (`id`)
);

ALTER TABLE `dog` ADD CONSTRAINT FOREIGN KEY (`id`) REFERENCES `animal`(`id`);

我首先尝试使用Doctrine具体继承,因为它似乎是这个问题的合理答案:

这是YAML文件:

Animal:
  columns:
    id: { primary: true , type: integer ,  autoincrement: true }
    color: { type: string(20) , notnull: true }
Dog:
  columns:
    breed: { type: string(20) , notnull: true }
  inheritance:
    extends: Animal
    type: concrete

结果SQL是:

CREATE TABLE `animal` (
  `id` BIGINT NOT NULL AUTO_INCREMENT,
  `color` VARCHAR(20) NOT NULL,
  PRIMARY KEY (`id`)
);

CREATE TABLE IF NOT EXISTS `dog` (
  `id` BIGINT NOT NULL AUTO_INCREMENT,
  `color` VARCHAR(20) NOT NULL,
  `breed` VARCHAR(20) NOT NULL,
  PRIMARY KEY (`id`)
);

color列的重复是可以的,但是外键在哪里?如何使用dog.id确保animal.id的完整性?如果我删除animal的行会怎样?

所以我尝试使用简单的一对一关联:

Animal:
  columns:
    id: { primary: true , type: integer(10) , autoincrement: true }
    color: { type: string(20) , notnull: true }
Dog:
  columns:
    animal_id: { primary: true , type: integer(10) }
    breed: { type: string(20) , notnull: true }
  relations:
    AnimalRecord:
      class: Animal
      foreignAlias: DogRecord
      type: one
      foreignType: one
      local: animal_id
      foreign: id

结果与上面相同(除了color列没有重复,这是正常的,因为继承不再被解释),仍然没有外键。

如果我只是将animal_idPRIMARY更改为UNIQUE,则会将外键从dog.animal_id创建为animal.id,但会自动增加新的自动增量id出现。

这一切都表现为PRIMARYFOREIGN KEY对于列而言是独占的,我无法理解为什么。此外,在我看来,这似乎是一个危险的缺陷。

1 个答案:

答案 0 :(得分:1)

您可能想要重新阅读有关具体继承的文档:动物表将始终为空,因为当您使用此继承策略时,您将给出关于狗“medor”的所有数据将存储在dog表中类型。这就是为什么没有必要在动物和狗类之间建立关系。

如果愿意,动物就像一个抽象类。

对我而言,您的架构应如下所示:

Animal:
  columns:
    id: { primary: true , type: integer(10) , autoincrement: true }
    color: { type: string(20) , notnull: true }
Dog:
  columns:
    breed: { type: string(20) , notnull: true }
相关问题