英文:
Why is foreign key constraint not added groovy/grails
问题
当我运行Groovy/Grails应用程序时,地址字段和访问地址字段在数据库中正确地创建了外键约束。但是,当我添加了"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
}
}
表的结构如下:
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 |
英文:
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 |
答案1
得分: 0
需要一个默认值。
添加:
static mapping = {
court defaultValue: "1"
}
英文:
Required a defaultvalue.
Add:
static mapping = {
court defaultValue: "1"
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论