ForeignKey

Enum ForeignKey 

Source
pub enum ForeignKey<T: Model> {
    PrimaryKey(T::PrimaryKey),
    Model(Box<T>),
}
Available on crate feature 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>

Source

pub fn primary_key(&self) -> &T::PrimaryKey

Returns the primary key of the referenced model.

Source

pub fn model(&self) -> Option<&T>

Returns the model, if it has been stored in this ForeignKey instance, or None otherwise.

Source

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.

Source

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>
where T: Model, <T as Model>::PrimaryKey: AsFormField,

Source§

type Type = <<T as Model>::PrimaryKey as AsFormField>::Type

The form field type associated with the field.
Source§

fn new_field( options: FormFieldOptions, custom_options: <Self::Type as FormField>::CustomOptions, ) -> Self::Type

Creates a new form field with the given options and custom options. Read more
Source§

fn clean_value(field: &Self::Type) -> Result<Self, FormFieldValidationError>
where Self: Sized,

Validates the value of the field and converts it to the final type. This method should return an error if the value is invalid. Read more
Source§

fn to_field_value(&self) -> String

Returns 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,

Source§

fn clone(&self) -> ForeignKey<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Model + Send + Sync> DatabaseField for ForeignKey<T>

Source§

const NULLABLE: bool = <T::PrimaryKey>::NULLABLE

Whether the field can be NULL in the database. Read more
Source§

const TYPE: ColumnType = <T::PrimaryKey>::TYPE

The type of the column in the database as one of the variants of the ColumnType enum. Read more
Source§

impl<T: Debug + Model> Debug for ForeignKey<T>
where T::PrimaryKey: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Model> From<&T> for ForeignKey<T>

Source§

fn from(model: &T) -> Self

Converts to this type from the input type.
Source§

impl<T: Model> From<T> for ForeignKey<T>

Source§

fn from(model: T) -> Self

Converts to this type from the input type.
Source§

impl<T: Model + Send + Sync> FromDbValue for ForeignKey<T>

Source§

fn from_sqlite(value: SqliteValueRef<'_>) -> Result<Self>

Available on crate feature sqlite only.
Converts the given SQLite database value to a Rust value. Read more
Source§

fn from_postgres(value: PostgresValueRef<'_>) -> Result<Self>

Available on crate feature postgres only.
Converts the given PostgreSQL database value to a Rust value. Read more
Source§

fn from_mysql(value: MySqlValueRef<'_>) -> Result<Self>

Available on crate feature mysql only.
Converts the given MySQL database value to a Rust value. Read more
Source§

impl<T: Model + Send + Sync> IntoField<ForeignKey<T>> for &T

Source§

fn into_field(self) -> ForeignKey<T>

Converts the type to the field type.
Source§

impl<T: Model> PartialEq for ForeignKey<T>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Model + Send + Sync> ToDbFieldValue for ForeignKey<T>

Source§

fn to_db_field_value(&self) -> DbFieldValue

Converts the Rust value to a DbFieldValue that indicates whether the value should be automatically generated by the database, or contains a specific, explicitly provided value.
Source§

impl<T: Model> Eq for ForeignKey<T>
where T::PrimaryKey: Eq,

Auto Trait Implementations§

§

impl<T> Freeze for ForeignKey<T>
where <T as Model>::PrimaryKey: Freeze,

§

impl<T> RefUnwindSafe for ForeignKey<T>

§

impl<T> Send for ForeignKey<T>
where <T as Model>::PrimaryKey: Send,

§

impl<T> Sync for ForeignKey<T>
where <T as Model>::PrimaryKey: Sync, T: Sync,

§

impl<T> Unpin for ForeignKey<T>
where <T as Model>::PrimaryKey: Unpin,

§

impl<T> UnwindSafe for ForeignKey<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Chain<T> for T

Source§

fn len(&self) -> usize

The number of items that this chain link consists of.
Source§

fn append_to(self, v: &mut Vec<T>)

Append the elements in this link to the chain.
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Container<T> for T
where T: Clone,

Source§

type Iter = Once<T>

An iterator over the items within this container, by value.
Source§

fn get_iter(&self) -> <T as Container<T>>::Iter

Iterate over the elements of the container (using internal iteration because GATs are unstable).
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> Fake for T

Source§

fn fake<U>(&self) -> U
where Self: FakeBase<U>,

Source§

fn fake_with_rng<U, R>(&self, rng: &mut R) -> U
where R: Rng + ?Sized, Self: FakeBase<U>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoApi for T

Source§

fn into_api<A>(self) -> UseApi<T, A>

into UseApi
Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> IntoField<Auto<T>> for T

Source§

fn into_field(self) -> Auto<T>

Available on crate feature db only.
Converts the type to the field type.
Source§

impl<T> IntoField<ForeignKey<T>> for T
where T: Model + Send + Sync,

Source§

fn into_field(self) -> ForeignKey<T>

Available on crate feature db only.
Converts the type to the field type.
Source§

impl<T> IntoField<T> for T
where T: ToDbFieldValue,

Source§

fn into_field(self) -> T

Available on crate feature db only.
Converts the type to the field type.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> OrderedContainer<T> for T
where T: Clone,