简体   繁体   中英

org.postgresql.util.PSQLException: ERROR: null value in column “id” violates not-null constraint

I looked around this error extensively on Google/MyBatis forums but could not find a definitive answer. I have table defined as below:

CREATE TABLE "user" 
(
 id integer NOT NULL DEFAULT nextval('user_userid_seq'::regclass),
 username character varying(256) NOT NULL,
 first_name character varying(128),
 last_name character varying(128),
 id_number character varying(32),
 id_type integer,
 email_verified boolean,
 cellphone_verified boolean,
 token character varying(256),
 CONSTRAINT pk_id PRIMARY KEY (id),
 CONSTRAINT unique_user_username UNIQUE (username)
)
WITH (
 OIDS=FALSE
);

The user_userid_seq is defined as:

     CREATE SEQUENCE user_userid_seq
     INCREMENT 1
     MINVALUE 1
     MAXVALUE 9223372036854775807
     START 1
    CACHE 1;

I use MyBatis generator to generate MapperXML and other classes. However, when I'm trying to insert a record using the auto generated mapper, I get the following error:

Error updating database. Cause: org.postgresql.util.PSQLException: 
  ERROR: null value in column "id" violates not-null constraint 
  Detail: Failing row contains (null, abc@gmail.com, Edwin, Eliot, 98098098, 1, f, null, null). 
  The error may involve com.dukstra.xxx.web.data.mapper.UserMapper.insert-Inline 
  The error occurred while setting parameters 
    SQL: insert into public.user (id, username, first_name, last_name, id_number, id_type, email_verified, cellphone_verified, token ) values (?, ?, ?, ?, ?, ?, ?, ?, ? ) 
  Cause: org.postgresql.util.PSQLException: 
    ERROR: null value in column "id" violates not-null constraint 
    Detail: Failing row contains (null, abc@gmail.com, Edwin, Eliot, 98098098, 1, f, null, null).

It seems that upon calling mapper.insert(newUser); the id column is being is passed as null, which causes the error above. What is the best way to fix this? I found several work arounds, but not sure if there is a definitive way? Some solutions suggested specifying an explicit selectKey and setting type to "pre" with the "generatedKey" option. However, this would mean having to write/customize xml for each of the table with a auto generated primary key with sequence. Is there a simpler way to either tell Postgres to automatically use a default key if the value is null or perhaps allow a "DEFAULT" (String) value in the ID field of generated user object? What is the best way to handle this?

UPDATE: here is the generator config:

 <?xml version="1.0" encoding="UTF-8" ?>
 <!DOCTYPE configuration
           PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
           "http://mybatis.org/dtd/mybatis-3-config.dtd">
     <configuration>
  <settings>
    <setting name="useGeneratedKeys" value="true" />
   </settings>
    <environments default="development">
    <environment id="development">
        <transactionManager type="JDBC" />
        <dataSource type="POOLED">
            <property name="driver" value="org.postgresql.Driver" />
            <property name="url" value="jdbc:postgresql://localhost:5433/travelnet" /> 
            <property name="username" value="postgres" />
            <property name="password" value="*******" />

        </dataSource>
    </environment>
</environments>

<mappers>
    <package name="com.dukstra.travelnet.web.data.mapper" />

</mappers>

I think the best way is to not pass the id column down to the DB when doing an insert. New records will get their id values from the sequence which you have there. How exactly you can achieve this with these particular technologies you're using, I am not sure as I have no experience with them. Probably you can do this through the MapperXML or through some annotations in the entity classes which you have.

use BIGSERIAL instead of creating manual sequence.

CREATE TABLE "user" 
(
 id BIGSERIAL,
 username character varying(256) NOT NULL,
 first_name character varying(128),
 last_name character varying(128),
 id_number character varying(32),
 id_type integer,
 email_verified boolean,
 cellphone_verified boolean,
 token character varying(256),
 CONSTRAINT pk_id PRIMARY KEY (id),
 CONSTRAINT unique_user_username UNIQUE (username)
)
WITH (
 OIDS=FALSE
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM