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:

  1. Unique constraints on one column or composite constraints on multiple columns as needed
  2. Set nullable or a default value if the column is optional
  3. Avoid referencing the model, like Post::all() since this can get out of step with the $table property for where the model looks for its data. Use the DB facade instead.
  4. 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()
  5. Do the migrations run up and down?

Discussions

Login to Post Comments
No Comments Posted