外键与 Dotrine 具体继承的关系
在我的架构中,我有一个通用表 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.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_id
从 PRIMARY
更改为 UNIQUE
,则外键将从 dog.animal_id
创建为 < code>animal.id,但会出现一个新的自动增量 id
。
这一切的行为就像是 PRIMARY
或 FOREIGN 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要重新阅读有关具体继承的文档:动物表将始终为空,因为当您使用此继承策略类型时,您提供的有关狗“medor”的所有数据都将存储在狗表中。这就是为什么在动物和狗类之间建立关系是没有意义的。
如果你愿意的话,Animal 就像一个抽象类。
对我来说,您的架构应该如下所示:
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: