Discourse superior 3.2.0 (v3.3.3,…).
Estoy usando PostgreSQL 15.3 (EnterpriseDB Advanced Server 15.3.0) en x86_64-pc-linux-gnu, compilado por gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-16), 64 bits.
Cuando ejecuto rake db:migrate, encuentro este error:
PG::NotNullViolation: ERROR: se viola la restricción de valor nulo en la columna "first_unread_at" de la relación "user_stats" (PG::NotNullViolation)
DETALLE: La fila fallida contiene (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)
Investigué y descubrí que la razón es que la aplicación está intentando insertar un valor nulo en un campo que no es nulo con un valor predeterminado current_timestamp.
Mi solución:
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;
Pero hay muchos otros campos en los que no se pueden insertar valores, y no he podido encontrarlos y arreglarlos todos usando este método.