gpt4 book ai didi

postgresql - 为什么 Grails 默认生成非空列?

转载 作者:行者123 更新时间:2023-11-29 12:58:35 25 4
gpt4 key购买 nike

我正在尝试学习 Grails,但遇到了一个奇怪的问题。我创建了一个应用程序并将 PostgreSQL 设置为我的数据库。这是我的application.yml

dataSource:
pooled: true
jmxExport: true
driverClassName: org.postgresql.Driver
username: postgres
password: XxXxXxXxX

environments:
development:
dataSource:
dbCreate: update
url: jdbc:postgresql://localhost:5432/datarh
test:
dataSource:
dbCreate: update
url: jdbc:postgresql://localhost:5432/datarh
production:
dataSource:
dbCreate: update
url: jdbc:postgresql://localhost:5432/datarh
properties: <ommited>

然后,我创建了两个域类:

class Candidate {

String name
String linkedin
String email
String phone
String personalMessage
String pathCV
static hasMany = [skills: Skill]

static constraints = {

}
}

class Skill {

String skill
String description

static hasMany = [candidates: Candidate]
static belongsTo = Candidate

static constraints = {

}
}

好的,现在我启动了我的应用程序并检查了我创建的表。令我惊讶的是,我得到了这个:

CREATE TABLE candidate
(
id bigint NOT NULL,
version bigint NOT NULL,
email character varying(255) NOT NULL,
linkedin character varying(255) NOT NULL,
name character varying(255) NOT NULL,
pathcv character varying(255) NOT NULL,
personal_message character varying(255) NOT NULL,
phone character varying(255) NOT NULL,
CONSTRAINT candidate_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE candidate
OWNER TO postgres;

CREATE TABLE candidate_skills
(
candidate_id bigint NOT NULL,
skill_id bigint NOT NULL,
CONSTRAINT candidate_skills_pkey PRIMARY KEY (candidate_id, skill_id),
CONSTRAINT fk_941r0fvlnkcvq9r7jqn5awce4 FOREIGN KEY (candidate_id)
REFERENCES candidate (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fk_t537lxgp89abb42uf1robp1xp FOREIGN KEY (skill_id)
REFERENCES skill (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
OIDS=FALSE
);
ALTER TABLE candidate_skills
OWNER TO postgres;

CREATE TABLE skill
(
id bigint NOT NULL,
version bigint NOT NULL,
description character varying(255) NOT NULL,
skill character varying(255) NOT NULL,
CONSTRAINT skill_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE skill
OWNER TO postgres;

如您所见,所有表都是使用 NOT-NULL 创建的。我该如何解决这个问题?

我的环境:

  • PostgresSQL 9.4
  • Grails 版本:3.1.4
  • 常规版本:2.4.6
  • JVM 版本:1.8.0_77

最佳答案

您的持久属性默认为不可空。如果您希望它们可以为空,您可以明确说明:

class Skill {

String skill
String description

static hasMany = [candidates: Candidate]
static belongsTo = Candidate

static constraints = {
description nullable: true
skill nullable: true

// ...

}
}

关于postgresql - 为什么 Grails 默认生成非空列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36436641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com