pub enum ForeignKey<T: Model> {
PrimaryKey(T::PrimaryKey),
Model(Box<T>),
}db only.Expand description
A foreign key to another model.
Internally, this is represented either as a primary key (in case the model has not been retrieved from the database) or as the model itself.
§Examples
use cot::db::{Auto, Database, ForeignKey, Model, model};
use cot::response::Response;
#[model]
struct MyModel {
#[model(primary_key)]
id: Auto<i32>,
user: ForeignKey<User>,
}
#[model]
struct User {
#[model(primary_key)]
id: Auto<i32>,
}
async fn index(db: Database) -> cot::Result<Response> {
let mut user = User { id: Auto::auto() };
user.save(&db).await?;
let mut my_model = MyModel {
id: Auto::auto(),
user: ForeignKey::from(user),
};
my_model.save(&db).await?;
// ...
}Variants§
PrimaryKey(T::PrimaryKey)
The primary key of the referenced model; used when the model has not been retrieved from the database yet or when it’s unnecessary to store the entire model instance.
Model(Box<T>)
The referenced model.
Implementations§
Source§impl<T: Model> ForeignKey<T>
impl<T: Model> ForeignKey<T>
Sourcepub fn primary_key(&self) -> &T::PrimaryKey
pub fn primary_key(&self) -> &T::PrimaryKey
Returns the primary key of the referenced model.
Sourcepub fn model(&self) -> Option<&T>
pub fn model(&self) -> Option<&T>
Returns the model, if it has been stored in this ForeignKey
instance, or None otherwise.
Sourcepub fn unwrap(self) -> T
pub fn unwrap(self) -> T
Unwrap the foreign key, returning the model.
§Panics
Panics if the model has not been stored in this ForeignKey instance.
Sourcepub async fn get<DB: DatabaseBackend>(&mut self, db: &DB) -> Result<&T>
pub async fn get<DB: DatabaseBackend>(&mut self, db: &DB) -> Result<&T>
Retrieve the model from the database, if needed, and return it.
If the model has already been retrieved, this method will return it.
This method will replace the primary key with the model instance if
the primary key is stored in this ForeignKey instance.
§Errors
Returns a DatabaseError::ForeignKeyNotFound error if the model
could not be found in the database.
Returns an error if there was a problem communicating with the database.
Trait Implementations§
Source§impl<T> AsFormField for ForeignKey<T>
impl<T> AsFormField for ForeignKey<T>
Source§type Type = <<T as Model>::PrimaryKey as AsFormField>::Type
type Type = <<T as Model>::PrimaryKey as AsFormField>::Type
Source§fn new_field(
options: FormFieldOptions,
custom_options: <Self::Type as FormField>::CustomOptions,
) -> Self::Type
fn new_field( options: FormFieldOptions, custom_options: <Self::Type as FormField>::CustomOptions, ) -> Self::Type
Source§fn clean_value(field: &Self::Type) -> Result<Self, FormFieldValidationError>where
Self: Sized,
fn clean_value(field: &Self::Type) -> Result<Self, FormFieldValidationError>where
Self: Sized,
Source§fn to_field_value(&self) -> String
fn to_field_value(&self) -> String
self as a value that can be set with FormField::set_value.Source§impl<T: Clone + Model> Clone for ForeignKey<T>where
T::PrimaryKey: Clone,
impl<T: Clone + Model> Clone for ForeignKey<T>where
T::PrimaryKey: Clone,
Source§fn clone(&self) -> ForeignKey<T>
fn clone(&self) -> ForeignKey<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: Model + Send + Sync> DatabaseField for ForeignKey<T>
impl<T: Model + Send + Sync> DatabaseField for ForeignKey<T>
Source§impl<T: Debug + Model> Debug for ForeignKey<T>where
T::PrimaryKey: Debug,
impl<T: Debug + Model> Debug for ForeignKey<T>where
T::PrimaryKey: Debug,
Source§impl<T: Model> From<T> for ForeignKey<T>
impl<T: Model> From<T> for ForeignKey<T>
Source§impl<T: Model + Send + Sync> FromDbValue for ForeignKey<T>
impl<T: Model + Send + Sync> FromDbValue for ForeignKey<T>
Source§fn from_sqlite(value: SqliteValueRef<'_>) -> Result<Self>
fn from_sqlite(value: SqliteValueRef<'_>) -> Result<Self>
sqlite only.Source§fn from_postgres(value: PostgresValueRef<'_>) -> Result<Self>
fn from_postgres(value: PostgresValueRef<'_>) -> Result<Self>
postgres only.Source§fn from_mysql(value: MySqlValueRef<'_>) -> Result<Self>
fn from_mysql(value: MySqlValueRef<'_>) -> Result<Self>
mysql only.Source§impl<T: Model + Send + Sync> IntoField<ForeignKey<T>> for &T
impl<T: Model + Send + Sync> IntoField<ForeignKey<T>> for &T
Source§fn into_field(self) -> ForeignKey<T>
fn into_field(self) -> ForeignKey<T>
Source§impl<T: Model> PartialEq for ForeignKey<T>where
T::PrimaryKey: PartialEq,
impl<T: Model> PartialEq for ForeignKey<T>where
T::PrimaryKey: PartialEq,
Source§impl<T: Model + Send + Sync> ToDbFieldValue for ForeignKey<T>
impl<T: Model + Send + Sync> ToDbFieldValue for ForeignKey<T>
Source§fn to_db_field_value(&self) -> DbFieldValue
fn to_db_field_value(&self) -> DbFieldValue
DbFieldValue that indicates whether
the value should be automatically generated by the database, or
contains a specific, explicitly provided value.impl<T: Model> Eq for ForeignKey<T>where
T::PrimaryKey: Eq,
Auto Trait Implementations§
impl<T> Freeze for ForeignKey<T>
impl<T> RefUnwindSafe for ForeignKey<T>
impl<T> Send for ForeignKey<T>
impl<T> Sync for ForeignKey<T>
impl<T> Unpin for ForeignKey<T>
impl<T> UnwindSafe for ForeignKey<T>
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<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 moreSource§impl<T> IntoField<Auto<T>> for T
impl<T> IntoField<Auto<T>> for T
Source§fn into_field(self) -> Auto<T>
fn into_field(self) -> Auto<T>
db only.Source§impl<T> IntoField<ForeignKey<T>> for T
impl<T> IntoField<ForeignKey<T>> for T
Source§fn into_field(self) -> ForeignKey<T>
fn into_field(self) -> ForeignKey<T>
db only.Source§impl<T> IntoField<T> for Twhere
T: ToDbFieldValue,
impl<T> IntoField<T> for Twhere
T: ToDbFieldValue,
Source§fn into_field(self) -> T
fn into_field(self) -> T
db only.