Search code examples
javamysqldatabasegrailsgroovy

Why is foreign key constraint not added groovy/grails


When i run the groovy/grails app, the address field and the visitingAddres correctly creates foreign key constraints in the database. BUT, i added the court field and the app doesnt automatically create the foreign key constraint for that field.

class Municipality {

static hasMany = [ cases : Case ]
Court court
Address address
Address visitingAddress

    
static constraints = {

    
    address nullable: false
    visitingAddress nullable: false
    cases nullable: true
    court nullable: false 

}

  This is how the table is looking:       

     municipality | CREATE TABLE `municipality` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
   `address_id` bigint(20) NOT NULL,
  `visiting_address_id` bigint(20) NOT NULL,
  `court_id` bigint(20) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `FK_hdo14fu6i4yo9fma1bfd4jfh7` (`address_id`),
  KEY `FK_esxb2ag360tnvpvcwgntk65ys` (`visiting_address_id`),
  CONSTRAINT `FK_esxb2ag360tnvpvcwgntk65ys` FOREIGN KEY (`visiting_address_id`) REFERENCES `address` (`id`),
  CONSTRAINT `FK_hdo14fu6i4yo9fma1bfd4jfh7` FOREIGN KEY (`address_id`) REFERENCES `address` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1 |

Solution

  • Required a defaultvalue.

    Add:

    static mapping = { court defaultValue: "1" }