Php 关系理论中生成的唯一密钥
我试图在教义中的答案和问题表之间建立一种单一的关系。这些是基本的YAML模式 答案模式Php 关系理论中生成的唯一密钥,php,orm,doctrine,symfony,doctrine-orm,Php,Orm,Doctrine,Symfony,Doctrine Orm,我试图在教义中的答案和问题表之间建立一种单一的关系。这些是基本的YAML模式 答案模式 type: entity table: fs_answer fields: id: id: true type: integer unsigned: false nullable: false generator: strategy: IDENTITY questionId: type: int
type: entity
table: fs_answer
fields:
id:
id: true
type: integer
unsigned: false
nullable: false
generator:
strategy: IDENTITY
questionId:
type: integer
unsigned: false
nullable: false
column: question_id
body:
type: text
nullable: false
oneToOne:
question:
targetEntity: FsQuestion
cascade: { }
mappedBy: null
inversedBy: null
joinColumns:
question_id:
referencedColumnName: id
orphanRemoval: false
lifecycleCallbacks: { }
问题模式:
type: entity
table: fs_question
fields:
id:
id: true
type: integer
unsigned: false
nullable: false
generator:
strategy: IDENTITY
body:
type: text
nullable: false
oneToMany:
answer:
targetEntity: FsAnswer
cascade: { }
mappedBy: question
inversedBy: answers
joinColumns:
question_id:
referencedColumnName: id
orphanRemoval: false
lifecycleCallbacks: { }
当我用doctrine:schema:update
更新模式时,它生成下面的SQL代码,并将unique key
放在答案表中的'question\u id'中
CREATE TABLE IF NOT EXISTS `fs_answer` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`question_id` int(11) NOT NULL,
`body` longtext NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UNIQ_552D082B1E27F6BF` (`question_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
如何避免这种独特的关键因素?从逻辑上讲(一对多),答案表中的问题id不应该有唯一的键 每个答案只有一个问题,对吗?事实上,你把它定义为oneToOne某种程度上证实了这一点 看来你的东西搞砸了。很惊讶它没有引起错误 回顾条令手册中的示例:
实际上,它和下面的代码一样简单 问题:
oneToMany:
answer:
targetEntity: FsAnswer
mappedBy: question
cascade: ["persist"]
答复:
manyToOne:
question:
targetEntity: FsQuestion
inversedBy: answer
对,每个答案只有一个问题。实际上,外键是有效的,但是,有一个我不想要的唯一的键,你在告诉教条,从一个答案到另一个问题建立一个一对一的关系,同时从一个问题到另一个答案建立一个一对一的关系。只是不去上班。将OneToOne更改为ManyToOne,并确保正确定义了joinColumn。