# PostgreSQL Enterprise: NOT NULL制約とCURRENT\_TIMESTAMPデフォルトを持つレコードへのNULL挿入ができない

**URL:** <https://meta.discourse.org/t/unable-to-insert-null-into-a-not-null-default-current-timestamp-record-of-postgresql-enterprise/354557>\
**Category:** Bug\
**Created:** [2025 年 2 月 26 日午前 6:28 UTC](https://meta.discourse.org/t/unable-to-insert-null-into-a-not-null-default-current-timestamp-record-of-postgresql-enterprise/354557 "2025-02-26T06:28:37Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![Hiệp\_Nguyễn\_ngoc](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/hi%E1%BB%87p_nguy%E1%BB%85n_ngoc/32/478398_2.png) [@Hiệp\_Nguyễn\_ngoc](https://meta.discourse.org/u/Hi%E1%BB%87p_Nguy%E1%BB%85n_ngoc)\
**Post date:** [2025 年 2 月 26 日午前 6:28 UTC](https://meta.discourse.org/t/unable-to-insert-null-into-a-not-null-default-current-timestamp-record-of-postgresql-enterprise/354557/1 "2025-02-26T06:28:37Z")

</div>

Discourse higher 3.2.0 (v3.3.3,…).  
I am using **PostgreSQL 15.3 (EnterpriseDB Advanced Server 15.3.0) on x86\_64-pc-linux-gnu, compiled by gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-16), 64-bit**。  
When I run `rake db:migrate`, I encounter this error:

```plaintext
PG::NotNullViolation: ERROR: null value in column "first_unread_at" of relation "user_stats" violates not-null constraint (PG::NotNullViolation)
DETAIL: Failing row contains (130, 0, 0, 0, 0, 0, 0, 2025-02-25 03:27:48.934913, null, null, 0, 0, 0, null, 0, 0, 0, null, 0, null, null, null, 0, 0)

```

I investigated and found out that the reason is the application is trying to insert a null value into a field that is not null with a **default current\_timestamp**.  
My solution:

```plaintext
BEGIN;
ALTER TABLE user_stats 
  ALTER COLUMN first_unread_at SET NOT NULL,
  ALTER COLUMN first_unread_at SET DEFAULT NOW();
COMMIT;

BEGIN;
ALTER TABLE user_stats 
  ALTER COLUMN first_unread_pm_at SET NOT NULL,
  ALTER COLUMN first_unread_pm_at SET DEFAULT NOW();
COMMIT;

BEGIN;
ALTER TABLE group_users 
  ALTER COLUMN first_unread_pm_at SET NOT NULL,
  ALTER COLUMN first_unread_pm_at SET DEFAULT NOW();
COMMIT;

```

But there are many other fields that can’t be inserted, I haven’t been able to find and fix them all using this method.

---

_[View the full topic](https://meta.discourse.org/t/unable-to-insert-null-into-a-not-null-default-current-timestamp-record-of-postgresql-enterprise/354557)._
