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:
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:
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.