Files
his/sql/remove_not_null_constraint_adm_practitioner_create_by.sql

35 lines
1.3 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- 移除 adm_practitioner 表中 create_by 列的 NOT NULL 约束
-- 用于解决 org.postgresql.util.PSQLException: ERROR: null value in column "create_by" of relation "adm_practitioner" violates not-null constraint
-- 在PostgreSQL中NOT NULL约束实际上是列的一个属性而不是命名约束
-- 因此我们使用 ALTER COLUMN ... DROP NOT NULL 来移除它
-- 可选:先查看当前表结构及约束信息
-- 注意:\d 命令仅在 psql 中有效,在脚本中不能使用
/*
SELECT
c.column_name,
c.is_nullable,
c.data_type,
tc.constraint_type
FROM
information_schema.columns c
LEFT JOIN
information_schema.constraint_column_usage ccu ON c.column_name = ccu.column_name AND c.table_name = ccu.table_name
LEFT JOIN
information_schema.table_constraints tc ON ccu.constraint_name = tc.constraint_name
WHERE
c.table_name = 'adm_practitioner'
AND c.column_name = 'create_by';
*/
-- 移除 create_by 列的 NOT NULL 约束
ALTER TABLE "public"."adm_practitioner"
ALTER COLUMN "create_by" DROP NOT NULL;
-- 可选:如果需要,可以同时移除默认值
-- ALTER TABLE "public"."adm_practitioner"
-- ALTER COLUMN "create_by" DROP DEFAULT;
-- 提示执行此脚本后create_by 列将允许 NULL 值
-- 这将解决插入数据时因缺少 create_by 值而导致的违反非空约束错误