2010-07-20 103 views
0

这是一个奇怪的一个无法删除的对象,由于外键约束

采用此架构:

Contact: 
    actAs: [Timestampable,SoftDelete] 
    columns: 
    first_name: { type: string(255), notnull: true } 
    second_name: { type: string(255), notnull: true } 
    relations: 
    Forums: 
     class: Forum 
     refClass: ContactForum 
     local: forum_id 
     foreign: contact_id 
     foreignAlias: Contacts 
    ContactForums: 
     local: id 
     foreign: contact_id 
     class: ContactForum 
     type: many 
     foreignType: one 
     cascade: [delete] 

Forum: 
    actAs: [Timestampable,SoftDelete] 
    columns: 
    name: { type: string(255), notnull: true } 
    relations: 
    ContactForums: 
     class: ContactForum 
     local: id 
     foreign: forum_id 
     type: many 
     cascade: [delete] 

ContactForum: 
    actAs: [Timestampable] 
    columns: 
    contact_id: { type: integer, primary: true } 
    forum_id: { type: integer, primary: true } 

然后,如果我们到Contact对象几个Forum对象的关联,然后试着删除此Contact对象,我们收到此错误信息:

完整性约束违规:19 contact_forum.created_at可能无法 NULL

如果您将SoftDelete添加到链接表中,则删除工作正常,SoftDelete也会正常工作。但是,我们不希望链接表上的SoftDelete,因为它意味着我们的主键无法正常工作。这是一个错误?

回答

0

我认为你的多对多关系的id会被搞砸,假设交响乐使用Doctrine 1.2.2。试试这个:

Contact: 
    actAs: [Timestampable,SoftDelete] 
    columns: 
    first_name: { type: string(255), notnull: true } 
    second_name: { type: string(255), notnull: true } 
    relations: 
    Forums: 
     refClass: ContactForum 
     local: contact_id 
     foreign: forum_id 
     cascade: [delete] 

Forum: 
    actAs: [Timestampable,SoftDelete] 
    columns: 
    name: { type: string(255), notnull: true } 
    relations: 
    Contacts: 
     refClass: ContactForum 
     local: forum_id 
     foreign: contact_id 
     cascade: [delete] 

ContactForum: 
    actAs: [Timestampable] 
    columns: 
    contact_id: { type: integer, primary: true } 
    forum_id: { type: integer, primary: true } 

的关系,指定与refClass,localforeign类时表示“这代表我对这个其他类的表列”和“这个其他类表中的列表示其他“。

编辑:我不确定您在联系人下的论坛关系的定义是否正确。假设你只需要一个多对多的关系,它可以被删除。

双编辑:看。以下是适当运作和级联多对多关系所需的所有模式。您不应该需要定义两个关系才能正确级联删除。

+0

模式是正确的。我不需要在论坛表上定义rel。问题在于联系人表和链接表。第二种关系被定义为确保删除级联到链接表。这是什么突破。将软删除添加到链接表,并没有问题。我想使用应用程序删除而不是数据库级联删除。 – johnwards 2010-07-20 21:43:32

+0

如果您不想使用db级联删除,则不要在模式中使用cascade:[delete]。它生成sql。 另外,我见过的使用Doctrine编写多对多关系的每个源代码都没有按照自己的方式定义它们。如果你确定这就是你想要的,那么很好,但它确实看起来像你有一些多余的,如果没有不正确的东西那里。 – jeremiahd 2010-07-21 03:55:24

+2

'级联:[删除]'是应用程序删除。 'onDelete:[CASCADE]'是db级联删除。见http://www.doctrine-project.org/projects/orm/1.2/docs/manual/defining-models%3Atransitive-persistence%3Adatabase-level-cascades/en – richsage 2010-07-21 07:40:19