humungs
humungs

Reputation: 1194

Why Grails is generating not-null columns by default?

I am trying to learn Grails and I faced a weird problem. I created an application and I setup PostgreSQL as my database. Here is my 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>

Then, I created two domain classes:

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 = {

    }
}

Ok, now I started my application and I went check my the tables created. For my surprise, I got this:

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;

As you can see, all table were created with NOT-NULL. How can I solve this?

My enviroment:

Upvotes: 1

Views: 157

Answers (1)

Jeff Scott Brown
Jeff Scott Brown

Reputation: 27245

Your persistent properties default to non-nullable. If you want them to be nullable, you can be explicit about that:

class Skill {

    String skill
    String description

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

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

        // ...

    }
}

Upvotes: 2

Related Questions