Skip to content

Commit 78d9306

Browse files
update post_stats table migration: add 'non_negative_columns' constraint
1 parent 5c740df commit 78d9306

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

‎database/migrations/2026_10_30_083501_create_post_stats_table.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Illuminate\Database\Migrations\Migration;
44
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\DB;
56
use Illuminate\Support\Facades\Schema;
67

78
return new class extends Migration
@@ -28,6 +29,18 @@ public function up(): void
2829
// Indexes
2930
$table->primary('post_id');
3031
});
32+
33+
// Add the non-negative constraint
34+
DB::statement(
35+
'ALTER TABLE post_stats
36+
ADD CONSTRAINT non_negative_columns
37+
CHECK (
38+
comments_count >= 0
39+
AND likes_count >= 0
40+
AND shares_count >= 0
41+
AND views_count >= 0
42+
)'
43+
);
3144
}
3245

3346
/**

0 commit comments

Comments
 (0)