Discourse higher 3.2.0 (v3.3.3,…).
Estou usando PostgreSQL 15.3 (EnterpriseDB Advanced Server 15.3.0) em x86_64-pc-linux-gnu, compilado por gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-16), 64 bits.
Quando executo rake db:migrate, encontro este erro:
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)
Investiguei e descobri que o motivo é que a aplicação está tentando inserir um valor nulo em um campo que não é nulo com um default current_timestamp.
Minha solução:
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;
Mas existem muitos outros campos que não podem ser inseridos, ainda não consegui encontrar e corrigir todos usando este método.