2016-04-05 35 views
1

我想學習Grails,我遇到了一個奇怪的問題。我創建了一個應用程序,並將PostgreSQL設置爲我的數據庫。這裏是我的application.yml爲什麼Grails默認生成非空列?

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創建的。我該如何解決這個問題?

我的環境:

  • PostgreSQL的9.4
  • Grails的版本:3.1.4
  • Groovy的版本:2.4.6
  • JVM版本:1.8.0_77

回答

2

你的持久化屬性默認爲不可空。如果你想他們是空的,你可以明確有關:

class Skill { 

    String skill 
    String description 

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

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

     // ... 

    } 
} 
+0

你也可以設置了作爲一個全球性的約束,如果你真的想要的一切默認爲空。有關語法和詳細信息,請參閱http://grails.github.io/grails-doc/3.1.4/guide/validation.html#sharingConstraints。 –

+0

對不起!我使用了一些其他框架,這種行爲是顛倒的(默認情況下爲'nullable = true')。 –

相關問題