nullable but not null
9 months ago
- #schema
- #database
- #migration
- Adding a new field to a model often starts as nullable to avoid table locks during migration.
- Application logic is updated to populate the field, followed by a backfill job for existing records.
- The final step of making the field non-nullable is often forgotten, leading to schema-data mismatch.
- Leaving fields nullable when they shouldn't be creates confusion, missed constraints, and unnecessary complexity.
- A script is provided to identify nullable fields that could be made non-nullable by checking actual null percentages.
- The script scans models for nullable fields and calculates the percentage of rows with null values.
- Fields with 0% nulls should be updated to non-nullable to improve schema accuracy and simplify code.
- Example output shows fields with their null percentages, helping identify candidates for schema cleanup.
- Fixing these issues ensures the data model is honest, improves validation, and keeps the application healthier.