当我运行groovy / grails应用程序时,地址字段和VisitingAddres会在数据库中正确创建外键约束。但是,我添加了Court字段,但该应用程序不会自动为该字段创建外键约束。
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 |
最佳答案
需要一个默认值。
加:
静态映射= {
法院默认值:“1”
}
关于java - 为什么不添加外键约束groovy/grails,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/64143267/