Rules Hub
Coding Rules Library
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
backend ruleP1universalStack: SQL / Sequelize migrations
databasemigrationnot-nulldata-integrity
Add NOT NULL columns in stages: add nullable, backfill, then add constraint
Adding a NOT NULL column to a populated table fails without a default; add the column nullable, fill it with data, then apply the not-null constraint.
PR: vinify-backend · org-mining-hist-2026-06Created: Jun 19, 2026
Bad example
Old codejavascript
| 1 | await queryInterface.addColumn('stock_count_record', 'previous_quantity', { |
| 2 | type: Sequelize.INTEGER, |
| 3 | allowNull: false, // fails on existing rows |
| 4 | }); |
Explanation (EN)
Objašnjenje (HR)
Good example
New codejavascript
| 1 | await queryInterface.addColumn('stock_count_record', 'previous_quantity', { type: Sequelize.INTEGER, allowNull: true }); |
| 2 | await queryInterface.sequelize.query('UPDATE stock_count_record SET previous_quantity = 0;'); |
| 3 | await queryInterface.changeColumn('stock_count_record', 'previous_quantity', { type: Sequelize.INTEGER, allowNull: false }); |
Explanation (EN)
Objašnjenje (HR)