title: Cannot delete or update a parent row: a foreign key constraint in grails with mysql
body: <p>I have Subscriber and Contact domain and a subscriber can have many contacts(one to many) in grails app. When I try to delete data from table contact it throws error like <code>DBCExceptionReporter Cannot delete or update a parent row: a foreign key constraint fails (</code>vprocure5<code>.</code>subscriber_contact<code>, CONSTRAINT</code>FKC5D3AF49E9F29F5<code>FOREIGN KEY (</code>contact_id<code>) REFERENCES</code>contact<code>(</code>id<code>))</code>.</p>
  
  <p>According to error message, I can not delete a parent row, but actually I am trying to delete contact data which are children of "Subscriber" domain. Here Subscriber should be parent and Contact should be child domain if I am not wrong.</p>
  
  <p><em>Subscriber domain</em></p>
  
  <pre><code>static hasMany= [contacts: Contact ]
  </code></pre>
  
  <p><em>Contact domain</em></p>
  
  <pre><code>static belongsTo = [Subscriber ]
  </code></pre>
  
  <p><em>ContactController.grooby</em></p>
  
  <pre><code>package com.vproc.member
  
  import org.springframework.dao.DataIntegrityViolationException
  
  class ContactController {
  
    def springSecurityService
      def subscriberService
    def imageUploadService
    def searchableService
    def autoCompleteService
  
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
  
  
    def index() {
      redirect(action: "list", params: params)
    }
  
    def list() {
  
      Subscriber loggedinSubscriber = Subscriber.get( springSecurityService.principal.id )
      List&lt;Contact&gt;contactsList = new ArrayList&lt;Contact&gt;();
      loggedinSubscriber?.contacts.each { it -&gt;
          contactsList.add(it)
      }
      [contactInstanceList:contactsList , contactInstanceTotal: contactsList.size() ]
    }
  
    def create() {
      [contactInstance: new Contact(params)]
    }
  
    def save() {
  
      if (params.birthday){
          params.birthday = (new SimpleDateFormat("MM/dd/yyyy")).parse(params.birthday)
      }
      def contactInstance = new Contact(params)
      Subscriber loggedinSubscriber = Subscriber.get( springSecurityService.principal.id )
      if (loggedinSubscriber == null)
        System.out.println("not able to save")
  
      else {
        if (!loggedinSubscriber.contacts){
          loggedinSubscriber.contacts = new ArrayList&lt;Contact&gt;();
        }
        loggedinSubscriber.contacts.add(contactInstance)
  
        if (!loggedinSubscriber.save(flush: true)) {
          flash.message = message(code: 'default.created.message', args: [message(code: 'contact.label', default: 'Contact'), contactInstance.id])
          render(view: "create", model: [contactInstance: contactInstance])
          return
        }
      }
  
      flash.message = message(code: 'default.created.message', args: [message(code: 'contact.label', default: 'Contact'), contactInstance.id])
      redirect(action: "list")
    }
  
  
     def ajaxDelete = {
  
        def contactInstance = Contact.get( params.id );
          contactInstance.tags.clear();
        println "=========================="
        if(contactInstance) {
          try {
            println "+++++++++++++++++++++++++"
            contactInstance.delete(flush:true)
  
            render "contact ${params.id} deleted"
  
          }
  
          catch(org.springframework.dao.DataIntegrityViolationException e) {
            render "contact ${params.id} could not be deleted"
          }
  
        }
        else {
          flash.message = "contact not found with id ${params.id}"
          render(action:list)
        }
      }
  </code></pre>
  
  <p><em>Contact.groovy</em></p>
  
  <pre><code>package com.vproc.member
  
  import java.util.Date;
  
  import com.vproc.common.Tag;
  import com.vproc.enquiry.ContactType;
  import grails.converters.JSON;
  
  class Contact {
  
      String name
      String phoneNumber
      String emailAddress
      Gender gender
      String url
      String note
      byte[] image
      String address
      Date dateCreated
      Date lastUpdated
      ContactType contactType
      Date birthday
  
      static belongsTo = [Subscriber ]
      static hasMany = [tags:Tag ,   shares: SharedContact]
      static  constraints = {
          image nullable: true
          phoneNumber nullable: true
          url nullable :true
          address nullable :true
          gender nullable :true
          note nullable :true
          contactType nullable :true
          birthday nullable :true
  
      }
  
      static mapping = {
      tags cascade: "all-delete-orphan"
  }
  
  
       //static searchable = [only: ['name', 'emailAddress']]
        static searchable = true
  
       static scaffold = true
      //static searchable = true
  }
  </code></pre>
  
  <p><em>Subscriber.groovy</em></p>
  
  <pre><code>package com.vproc.member
  
  import java.util.Date;
  
  
  
  class Subscriber extends PartyRole{
  
      transient springSecurityService
  
      String username
      String password
      boolean enabled
      boolean accountExpired
      boolean accountLocked
      boolean passwordExpired
      StatusEnum status
      Date dateCreated
      Date lastUpdated
      List&lt;Contact&gt; contacts ;
  
      static belongsTo = [ customer: Customer]
      static hasMany = [scontacts: Contact]
  
  
  
      static mapping = {
          password column: '`password`'
      }
  
      Set&lt;Role&gt; getAuthorities() {
          SubscriberRole.findAllBySubscriber(this).collect { it.role } as Set
      }
  
      def beforeInsert() {
          encodePassword()
      }
  
      def beforeUpdate() {
          if (isDirty('password')) {
              encodePassword()
          }
      }
  
      protected void encodePassword() {
          password = springSecurityService.encodePassword(password)
      }
  }
  </code></pre>
  
  <p>Any idea how I can delete contact records here?</p>
  
  <p><em>delete action in Contact controller</em></p>
  
  <pre><code>def ajaxDelete = {
        Subscriber loggedinSubscriber = Subscriber.get( springSecurityService.principal.id )
        def contactInstance = Contact.get( params.id );
        contactInstance.tags.clear();
        loggedinSubscriber.removeFromContacts(contactInstance)
        println "=========================="
        if(contactInstance) {
          try {
            println "+++++++++++++++++++++++++"
            contactInstance.delete(flush:true)
            render "contact ${params.id} deleted"
  
          }
  
          catch(org.springframework.dao.DataIntegrityViolationException e) {
            render "contact ${params.id} could not be deleted"
          }
  
        }
        else {
          flash.message = "contact not found with id ${params.id}"
          render(action:list)
        }
      }
  </code></pre>