PSQLException:ERROR:列中的空值违反了非空约束 [英] PSQLException: ERROR: null value in column violates not-null constraint

查看:9039
本文介绍了PSQLException:ERROR:列中的空值违反了非空约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PostgreSQL 8.4.13 on x86_64-pc-linux-gnu在Debian 4.4.5-8,64位。



我创建了以下表:

  CREATE TABLE用户(
user_id serial PRIMARY KEY NOT NULL,
name varchar ,
username varchar(150),
password varchar(150),
);然后,使用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();

这里的问题是executeUpdate()生成以下异常:

  org.postgresql.util.PSQLException:ERROR:列user_id中的空值违反非空约束



奇怪的是,如果我使用psql执行相同的insert语句,它会成功执行。



谢谢。

解决方案

p>作为@mu评论,错误消息与您的问题的其余部分矛盾。



唯一合理的解释是,事实上, >不同的表格



尝试:

  INSERT INTO用户(user_id,名称,用户名,密码)VALUES 
(1234,'foo','foo','foo');

检查你的表。INSERT是否到达你预期的表格?如果没有,请检查你的设置:




  • IP,端口,数据库名称?

  • 数据库中有相同的模式吗?检查您的 search_path 设置。

  • 您没有意外双引号表名称USERS?双引号标识符不转换为小写。有关详细信息,请阅读标识符和关键字一章。 。



查找表 users 的其他实例,完成了。 :)


I am using PostgreSQL 8.4.13 on x86_64-pc-linux-gnu on Debian 4.4.5-8, 64-bit.

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:

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:

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.

Any ideas would be much appreciated.

Thank you.

解决方案

As @mu commented, the error message contradicts the rest of your question.

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? If not, check your settings:

  • IP, port, db name?
  • Same schema in the DB? Check your search_path setting.
  • You did not by accident double quote the table name "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. :)

这篇关于PSQLException:ERROR:列中的空值违反了非空约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆