This repository was archived by the owner on Jul 16, 2021. It is now read-only.
This repository was archived by the owner on Jul 16, 2021. It is now read-only.
[Proposal] Add schema method storedAsNotNull
and virtualAsNotNull
#1211
Closed
Description
Currently ->storedAs()
and ->virtualAs()
always make them nullable.
But making generated columns non-nullable provides robust and flexible constraints.
Example
Goals
- We have
members
table andplans
tablemembers.role
takes one ofowner
guest
subscriber
members.plan_id
has value only whenmembers.role
issubscriber
- We also need to sort members in
owner
guest
subscriber
order.
Code
$table
->unsignedTinyInteger('sort_priority')
->storedAsNotNull("
CASE WHEN role = 'owner' AND plan_id IS NULL THEN 1
WHEN role = 'guest' AND plan_id IS NULL THEN 2
WHEN role = 'subscriber' AND plan_id IS NOT NULL THEN 3
ELSE NULL
END
")
->index();
members.sort_priority
allows us making fast sorting ORDER BY sort_priority, id
ensuring strong consistency.
Metadata
Metadata
Assignees
Labels
No labels