ERROR: column “*****“ of relation “*****“ contains null values

文章讲述了在已有数据的表中修改字段默认值为NOTNULL时遇到的错误,提出通过创建临时表,迁移数据并用COALESCE函数处理NULL值,最后重命名表的解决方案,强调备份数据的重要性。

在项目中有对于数据库的字段规范,要求数据库的一些字段要设置默认值且为not null状态,我很理所当然的写了如下的sql

ALTER TABLE 表名 ALTER COLUMN 字段名 SET NOT NULL, ALTER COLUMN 字段名 SET DEFAULT '***', ALTER COLUMN 字段名 SET NOT NULL, ALTER COLUMN 字段名 SET DEFAULT '***';

在大多数情况下,这个完全没有问题的,但是当我把所有的sql执行之后,发现,会有一些报错。错误:ERROR:  column "*****" of relation "*****" contains null values

这是什么原因呢?

这个sql语句,在数据库有数据的时候,如果对应的字段为null,那么你再使用此sql语句去修改为not null 并且设置默认值,就会出现上述错误,如果是空表,就没问题的。

那么怎么解决这个问题呢?

在数据库表中,当某个字段的值为 NULL 时,确实可以修改该字段的默认值。但是,如果你想将字段的默认值设置为 NOT NULL 约束,并且字段中已经存在 NULL 值,则需要进行一些额外的步骤来处理现有的 NULL 值。

下面是一种常见的方法来处理这种情况:

  1. 创建一个新的临时表,结构与原表完全相同,但不包含要修改的字段。
  2. 将原表中的数据复制到新表中,并使用 COALESCE 函数将 NULL 值替换为默认值。
  3. 删除原表。
  4. 将新表重命名为原表的名称。

以下是使用 SQL 语句的示例:

-- 创建新表  
CREATE TABLE temp_table AS  
SELECT *  
FROM original_table;  
  
-- 将 NULL 值替换为默认值  
UPDATE temp_table  
SET column_name = COALESCE(column_name, 'default_value')  
WHERE column_name IS NULL;  
  
-- 删除原表  
DROP TABLE original_table;  
  
-- 将新表重命名为原表的名称  
ALTER TABLE temp_table RENAME TO original_table;

请注意,上述示例中的 column_name 是要修改的字段名称,default_value 是要设置的默认值。你需要根据实际情况进行相应的更改。

这样,你就可以将字段的默认值设置为 NOT NULL 约束,并且不会丢失数据。但是,请谨慎操作,确保在执行任何修改之前备份数据或进行必要的测试。

请求地址'/mainOrderInfo/close/A000000004',发生未知异常. org.springframework.dao.DataIntegrityViolationException: ### Error updating database. Cause: org.postgresql.util.PSQLException: ERROR: null value in column "if_renew" of relation "wpp_so_close_record" violates not-null constraint 详细:Failing row contains (1986615684026159106, 000000, 1, 1, 2025-11-07 10:04:23.264, 1, zb, admin, 2025-11-07 10:04:23.264, 1, admin, zb, 103, 02003380820101, 0200338082, 01, 01, 1, admin, 06, null, null, null, null). ### The error may exist in org/dromara/wpp/mapper/WppSoCloseRecordMapper.java (best guess) ### The error may involve org.dromara.wpp.mapper.WppSoCloseRecordMapper.insert-Inline ### The error occurred while setting parameters ### SQL: INSERT INTO wpp_so_close_record (id, product_plan_order, so_no, so_line, so_plan_line, close_type, originator, close_reason, activity, dataversion, createdate, creater, create_user, create_user_code, updatedate, updater, update_user_code, last_modify_user, create_dept, tenant_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '000000') ### Cause: org.postgresql.util.PSQLException: ERROR: null value in column "if_renew" of relation "wpp_so_close_record" violates not-null constraint 详细:Failing row contains (1986615684026159106, 000000, 1, 1, 2025-11-07 10:04:23.264, 1, zb, admin, 2025-11-07 10:04:23.264, 1, admin, zb, 103, 02003380820101, 0200338082, 01, 01, 1, admin, 06, null, null, null, null). ; ERROR: null value in column "if_renew" of relation "wpp_so_close_record" violates not-null constraint 详细:Failing row contains (1986615684026159106, 000000, 1, 1, 2025-11-07 10:04:23.264, 1, zb, admin, 2025-11-07 10:04:23.264, 1, admin, zb, 103, 02003380820101, 0200338082, 01, 01, 1, admin, 06, null, null, null, null). at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:258) at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:107) at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:93) at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:347) at jdk.proxy2/jdk.proxy2.$Proxy188.insert(Unknown Source) at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:224) at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:59) at com.baomidou.mybatisplus.core.override.MybatisMapperProxy$PlainMethodInvoker.invoke(MybatisMapperProxy.java:156) at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:93) at jdk.proxy2/jdk.proxy2.$Proxy201.insert(Unknown Source) at com.baomidou.mybatisplus.core.mapper.BaseMapper.insertOrUpdate(BaseMapper.java:486) at java.base/java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:732) at com.baomidou.mybatisplus.core.override.MybatisMapperProxy$DefaultMethodInvoker.invoke(MybatisMapperProxy.java:182) at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:93) at jdk.proxy2/jdk.proxy2.$Proxy201.insertOrUpdate(Unknown Source) at org.dromara.wpp.service.impl.WppMainOrderInfoServiceImpl.getCloseType(WppMainOrderInfoServiceImpl.java:288) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568)
最新发布
11-08
2025-06-30 11:35:02.937 GMT [45398] STATEMENT: INSERT INTO sys_ports (agent_id, scan_id, scan_time, protocol, local_ip, local_port, remote_ip, remote_port, tx_queue, rx_queue, inode, state, PID, process, checksum, item_id) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16) ON CONFLICT (agent_id, protocol, local_ip, local_port, inode) DO UPDATE SET scan_id = excluded.scan_id, scan_time = excluded.scan_time, remote_ip = excluded.remote_ip, remote_port = excluded.remote_port, tx_queue = excluded.tx_queue, rx_queue = excluded.rx_queue, state = excluded.state, PID = excluded.PID, checksum = excluded.checksum, item_id = excluded.item_id; 2025-06-30 11:35:03.011 GMT [45398] ERROR: null value in column "local_ip" of relation "sys_ports" violates not-null constraint 2025-06-30 11:35:03.011 GMT [45398] DETAIL: Failing row contains (127337, 0, 2025/06/30 11:35:02, tcp6, null, 49161, ::, 0, 0, 0, 0, listening, 105, remoted, 4eb5bda8cc846a2e418696930378db1e5b5b348b, e7a6b4ecab29d8338bfca7e28ed9d49269aa4f83). 2025-06-30 11:35:03.011 GMT [45398] STATEMENT: INSERT INTO sys_ports (agent_id, scan_id, scan_time, protocol, local_ip, local_port, remote_ip, remote_port, tx_queue, rx_queue, inode, state, PID, process, checksum, item_id) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16) ON CONFLICT (agent_id, protocol, local_ip, local_port, inode) DO UPDATE SET scan_id = excluded.scan_id, scan_time = excluded.scan_time, remote_ip = excluded.remote_ip, remote_port = excluded.remote_port, tx_queue = excluded.tx_queue, rx_queue = excluded.rx_queue, state = excluded.state, PID = excluded.PID, checksum = excluded.checksum, item_id = excluded.item_id; 2025-06-30 11:35:03.029 GMT [45395] ERROR: null value in column "local_ip" of relation "sys_ports" violates not-null constraint 2025-06-30 11:35:03.029 GMT [45395] DETAIL: Failing row contains (127337, 0, 2025/06/30 11:35:02, tcp6, null, 49157, ::, 0, 0, 0, 0, listening, 105, remoted, 718e64c7b5deec25f0e57f6c4adfd839554b9d4c, ebbaea0b1606492e376b413d2e569bd77de951e0).
07-02
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值