为什么在Groovy/Grails中未添加外键约束?

huangapple go评论77阅读模式
英文:

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"
}

huangapple
  • 本文由 发表于 2020年10月1日 01:58:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64143267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定