Database Migrations
Paul Brown · 08/02/2024 · 1 min read
When writiing database migrations (or reviewing them) look out for/add/avoid the following things:
- Unique constraints on one column or composite constraints on multiple columns as needed
- Set nullable or a default value if the column is optional
- Avoid referencing the model, like
Post::all()since this can get out of step with the$tableproperty for where the model looks for its data. Use theDBfacade instead. - Be explicit about what happens when foreign-key-related records are deleted. In PHP's Laravel, for example, you can use
->onDelete('cascade')or->onDelete('null')or->cascadeOnDelete()or->nullOnDelete() - Do the migrations run up and down?
Discussions
Login to Post Comments