Faux database constraint
Paul Brown · 31/01/2024 · 1 min read
DB::statement('ALTER TABLE provisions ADD CHECK (IF(is_default, (SELECT COUNT(*) FROM provisions WHERE is_default = true) = 1, 1))');
// Unique constraint to ensure only one default per provision_type
$table->unique(['provision_id', 'provision_type']);
// Foreign key to provisions table
$table->foreign('provision_id')->references('id')->on('provisions')->onDelete('cascade');
Discussions
Login to Post Comments