johnwards
johnwards

Reputation: 1901

Can't delete objects due to foreign key constraints

This is a strange one

Take this schema:

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 }

Then if we associate a couple of Forum objects to a Contact object and then try and delete this Contact object we get this error message:

Integrity constraint violation: 19 contact_forum.created_at may not be NULL

If you add SoftDelete to the link table the delete works correctly, and so does the SoftDelete. However we don't want SoftDelete on the link table as it means our primary keys don't work correctly. Is this a bug?

Upvotes: 0

Views: 760

Answers (2)

johnwards
johnwards

Reputation: 1901

This is a doctrine bug. Bug report here: http://www.doctrine-project.org/jira/browse/DC-795 with patch to fix.

Upvotes: 1

jdd
jdd

Reputation: 4336

I think your ids for your many to many relationship are screwed up, assuming that symphony is using Doctrine 1.2.2. Try this:

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 }

In the relationship, when specifying the class with refClass, local and foreign mean "the column on this other class' table that represents me" and "the column on this other classes table that represents the other", respectively.

Edit: I'm not sure if your definition for the Forums relation under Contact is correct either. Assuming you just need a many-to-many relationship, it can be removed.

Double Edit: Look. Here is all the schema you should need for properly functioning and cascading many-to-many relationships. You shouldn't need two relationships defined to properly cascade deletes.

Upvotes: 0

Related Questions