简体   繁体   English

PSQLException:错误:列中的空值违反了非空约束

[英]PSQLException: ERROR: null value in column violates not-null constraint

I am using PostgreSQL 8.4.13 on x86_64-pc-linux-gnu on Debian 4.4.5-8, 64-bit.我在 64 位 Debian 4.4.5-8 上的 x86_64-pc-linux-gnu 上使用 PostgreSQL 8.4.13。

I have created the following table:我创建了下表:

CREATE TABLE users (
user_id             serial PRIMARY KEY NOT NULL,
name                varchar(200),
username            varchar(150),
password            varchar(150),
);

Then, using a Java application, I execute the following code:然后,使用 Java 应用程序,我执行以下代码:

String insertTableSQL = "INSERT INTO USERS"
                + "(name, username, password) VALUES"
                + "(?,?,?)";
PreparedStatement preparedStatement = DBCon.prepareStatement(insertTableSQL);
preparedStatement.setString(1, userInfo.get("name"));           
preparedStatement.setString(2, userInfo.get("username"));
preparedStatement.setString(3, userInfo.get("password")));

preparedStatement.executeUpdate();

The issue here is that the executeUpdate() generates the following exception:这里的问题是 executeUpdate() 生成以下异常:

org.postgresql.util.PSQLException: ERROR: null value in column "user_id" violates not-null constraint

The weird thing is that if I execute the same insert statement using psql, it executes successfully.奇怪的是,如果我使用 psql 执行相同的插入语句,它会成功执行。

Any ideas would be much appreciated.任何想法将不胜感激。

Thank you.谢谢你。

As @mu commented, the error message contradicts the rest of your question.正如@mu 评论的那样,错误消息与您的其余问题相矛盾。

The only reasonable explanation left is that you are, in fact, writing to a different table剩下的唯一合理解释是,实际上,您正在写入不同的表

Try:尝试:

INSERT INTO users (user_id, name, username, password) VALUES
(1234,'foo', 'foo', 'foo')";

And check your table.并检查你的桌子。 Did the INSERT arrive at the table you expected? INSERT 是否到达您预期的表? If not, check your settings:如果没有,请检查您的设置:

  • IP, port, db name? IP、端口、数据库名称?
  • Same schema in the DB?数据库中的架构相同吗? Check your search_path setting.检查您的search_path设置。
  • You did not by accident double quote the table name "USERS"?你是不是不小心把表名“USERS”用双引号引起来了? Double-quoted identifiers are not cast to lower case.双引号标识符不会转换为小写。 Read the chapter Identifiers and Key Words for details..有关详细信息,请阅读“ 标识符和关键字”一章。

Find the other instance of table users and fix potential damage you may have done.找到表users的其他实例并修复您可能造成的潜在损坏。 :) :)

String insertTableSQL = "INSERT INTO USERS"
            + "(name, username, password) VALUES"
            + "(?,?,?)";
PreparedStatement preparedStatement = DBCon.prepareStatement(insertTableSQL);
preparedStatement.setString(1, userInfo.get("name"));           
preparedStatement.setString(2, userInfo.get("username"));
preparedStatement.setString(3, userInfo.get("password")));

preparedStatement.executeUpdate();

Note only three question marks and now the fields line up correctly.请注意仅三个问号,现在字段正确排列。

Also don't worry about the NOT NULL constraint.也不要担心NOT NULL约束。 It will handle itself (how could it ever be NULL , it's SERIAL ) and could be causing your error.它会自行处理(它怎么可能是NULL ,它是SERIAL )并且可能会导致您的错误。 Just remove it.只需将其删除。

Resolved.解决。

The problem with me was that I was having 2 column in the database and in entity there was one mapped.我的问题是我在数据库中有 2 列,而在实体中有一个映射。 it happened due to sql update in properties file它是由于属性文件中的 sql 更新而发生的

once remove one and get fixed一旦删除并得到修复

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 突发错误:org.postgresql.util.PSQLException:错误:列“id”中的空值违反非空约束 - Sudden error: org.postgresql.util.PSQLException: ERROR: null value in column “id” violates not-null constraint org.postgresql.util.PSQLException:错误:“ category_id”列中的空值违反了非空约束 - org.postgresql.util.PSQLException: ERROR: null value in column “category_id” violates not-null constraint org.postgresql.util.PSQLException:错误:列“id”中的空值违反非空约束 - org.postgresql.util.PSQLException: ERROR: null value in column “id” violates not-null constraint PSQLException:错误:关系“床”的“idRoom”列中的 null 值违反非空约束 - PSQLException: ERROR: null value in column "idRoom" of relation "Bed" violates not-null constraint Spring 启动后 api - org.postgresql.util.PSQLException:错误:“密码”列中的 null 值违反了非空约束 - Spring boot post api - org.postgresql.util.PSQLException: ERROR: null value in column "password" violates not-null constraint 由以下原因引起:org.postgresql.util.PSQLException:错误:列“ student_id”中的空值违反了非空约束 - Caused by: org.postgresql.util.PSQLException: ERROR: null value in column “student_id” violates not-null constraint Spring Boot + PostgreSQL: 原因:org.postgresql.util.PSQLException:错误:“front_menu_id”列中的空值违反了非空约束 - Spring Boot + PostgreSQL: Cause: org.postgresql.util.PSQLException: ERROR: null value in column "front_menu_id" violates not-null constraint 如何修复列“id”中的'null值违反了非空约束'' - How to fix ''null value in column “id” violates not-null constraint'' 在带有posgres数据库的jbpm6.5.0Final中出现错误:列“ id”中的空值违反了非空约束 - In jbpm6.5.0Final with posgres database ERROR: null value in column “id” violates not-null constraint 一对多级联保存,错误:“ b_id”列中的空值违反了非空约束 - One-To-Many Cascade Save, ERROR: null value in column “b_id” violates not-null constraint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM