Created
August 1, 2018 00:20
-
-
Save hotmeteor/d49c1bc4d1377f6a1c8099dd188c8d86 to your computer and use it in GitHub Desktop.
Database column naming proposal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Support\Facades\Schema; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
class CreateMyTable extends Migration | |
{ | |
/** | |
* Run the migrations. | |
*/ | |
public function up() | |
{ | |
Schema::create('my_table', function (Blueprint $table) { | |
// Obvi, increments for `id` | |
$table->increments('id'); | |
// All foreign keys should be indexed | |
$table->integer('account_id')->index(); | |
// All columns that *cannot* be empty should *not* be nullable or have an empty default | |
$table->string('name'); | |
// All columns that *can* be empty should have nullable(), not default('') | |
$table->json('codes')->nullable(); | |
// All boolean columns should have a default value | |
// Booleans should always start with `is_` or `has_` | |
$table->boolean('is_approval_required')->default(false); | |
// Unit columns should include the unit | |
$table->integer('job_length_minutes')->nullable(); | |
// Foreign keys to a user should end in `_by` | |
$table->integer('approved_by')->index()->nullable(); | |
// Timestamps should end in `_at` | |
$table->timestamp('approved_at')->nullable(); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
*/ | |
public function down() | |
{ | |
// Down migrations should exist. | |
Schema::dropIfExists('my_table'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment