我正在尝试学习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创建的。我该如何解决这个问题?
我的环境:
答案 0 :(得分:2)
您的持久属性默认为不可为空。如果您希望它们可以为空,那么您可以明确指出:
class Skill {
String skill
String description
static hasMany = [candidates: Candidate]
static belongsTo = Candidate
static constraints = {
description nullable: true
skill nullable: true
// ...
}
}