外键与 Dotrine 具体继承的关系

发布于 2024-11-06 13:33:26 字数 2122 浏览 0 评论 0原文

在我的架构中,我有一个通用表 Animal 和一个继承表 Dog。 在使用原则之前,我曾经使用继承的 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< 的重复/code> 列没问题,但是外键在哪里?如何确保 dog.idanimal.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 创建为 < code>animal.id,但会出现一个新的自动增量 id

这一切的行为就像是 PRIMARYFOREIGN KEY 是列所独有的,我不明白为什么。此外,在我看来,这是一个危险的缺陷。

In my schema, I have a generic table Animal and an inherited table Dog.
Before using doctrine, I used to implement this pattern with an inherited id referencing the generic id as foreign key.
I can't reproduce the same with Doctrine, and I feel like something is missing.

The schema I used to produce is the following :

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`);

I first tried to use Doctrine concrete inheritance, as it seemed the logical answer to this problem :

Here is the YAML file :

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

And the resulting SQL is :

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`)
);

The duplication of the color column is OK, but where is the foreign key ? How do I ensure the integrity of my dog.id with my animal.id ? And what happens if I delete animal's rows ?

So I tried to use a simple one-to-one association :

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

The result is the same as above (except the color column isn't duplicated, which is normal since the inheritance isn't explicited anymore), still no foreign key.

If I just change the animal_id from PRIMARY to UNIQUE, the foreign key is created from dog.animal_id to animal.id, but a new autoincrement'ed id appears.

It all behaves like being PRIMARY or FOREIGN KEY are exclusive for a column, and I can't understand why. Furthermore, it seems to me like a dangerous flaw.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉风有信 2024-11-13 13:33:26

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

如果你愿意的话,Animal 就像一个抽象类。

对我来说,您的架构应该如下所示:

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

you may want to re-read the documentation on concrete inheritance : the animal table will always be empty, because all the data you will give about the dog "medor" will be stored in the dog table when you use this inheritance strategy type. That's why there is no point to create a relation between animal and dog classes.

Animal is like an abstract class if you will.

To me, your schema should look like this:

Animal:
  columns:
    id: { primary: true , type: integer(10) , autoincrement: true }
    color: { type: string(20) , notnull: true }
Dog:
  columns:
    breed: { type: string(20) , notnull: true }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文