pub enum OnDelete {
Cascade,
SetNull,
Restrict,
SetDefault,
NoAction,
}Expand description
Defines the behavior when a referenced object is deleted in a foreign key relationship.
This enum maps to SQL ON DELETE clauses and provides type-safe foreign key behavior.
§Examples
#[ormada_model(table = "posts")]
struct Post {
#[primary_key]
id: i32,
// Cascade: Delete posts when author is deleted
#[foreign_key(Author, on_delete = Cascade)]
author_id: i32,
// SetNull: Set category to NULL when category is deleted
#[foreign_key(Category, on_delete = SetNull)]
category_id: Option<i32>, // Must be Option for SetNull
}Variants§
Cascade
Delete related objects when the referenced object is deleted.
SQL: ON DELETE CASCADE
§Example
#[foreign_key(Author, on_delete = Cascade)]
author_id: i32,When an Author is deleted, all related Posts are automatically deleted.
SetNull
Set the foreign key to NULL when the referenced object is deleted.
SQL: ON DELETE SET NULL
Important: Field must be Option<T> when using SetNull.
§Example
#[foreign_key(Category, on_delete = SetNull)]
category_id: Option<i32>, // Must be Option!When a Category is deleted, all Posts with that category will have category_id set to NULL.
Restrict
Prevent deletion of the referenced object if any related objects exist.
SQL: ON DELETE RESTRICT
§Example
#[foreign_key(Author, on_delete = Restrict)]
author_id: i32,Attempting to delete an Author that has Posts will result in a database error.
SetDefault
Set the foreign key to its default value when the referenced object is deleted.
SQL: ON DELETE SET DEFAULT
§Example
#[foreign_key(Status, on_delete = SetDefault, default = 1)]
status_id: i32,When a Status is deleted, all related records will have status_id set to 1.
NoAction
Implementations§
Source§impl OnDelete
impl OnDelete
Sourcepub const fn to_sql(&self) -> &'static str
pub const fn to_sql(&self) -> &'static str
Convert the enum variant to its SQL string representation.
This is used during migration generation to create the appropriate SQL.
§Examples
use ormada::types::OnDelete;
assert_eq!(OnDelete::Cascade.to_sql(), "CASCADE");
assert_eq!(OnDelete::SetNull.to_sql(), "SET NULL");
assert_eq!(OnDelete::Restrict.to_sql(), "RESTRICT");Sourcepub const fn requires_nullable(&self) -> bool
pub const fn requires_nullable(&self) -> bool
Check if this OnDelete variant requires the field to be nullable.
Returns true for SetNull, false for all others.
§Examples
use ormada::types::OnDelete;
assert!(OnDelete::SetNull.requires_nullable());
assert!(!OnDelete::Cascade.requires_nullable());
assert!(!OnDelete::Restrict.requires_nullable());Trait Implementations§
impl Copy for OnDelete
impl Eq for OnDelete
impl StructuralPartialEq for OnDelete
Auto Trait Implementations§
impl Freeze for OnDelete
impl RefUnwindSafe for OnDelete
impl Send for OnDelete
impl Sync for OnDelete
impl Unpin for OnDelete
impl UnwindSafe for OnDelete
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more