Skip to main content

ForeignKey

Struct ForeignKey 

Source
pub struct ForeignKey<T: Model> { /* private fields */ }
Expand description

A many-to-one reference to T stored as T’s primary key.

Serializes as the bare key ("author": 7), like Django REST framework’s default. The cached object is never serialized. [Validate], [Schema] and [Dump] follow T::Pk.

Implementations§

Source§

impl<T: Model> ForeignKey<T>

Source

pub fn new(id: T::Pk) -> Self

Reference by primary key (nothing is loaded).

Source

pub fn to(object: T) -> Self

Reference an already loaded object; it becomes the cached value.

Source

pub fn id(&self) -> &T::Pk

The referenced primary key.

Source

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

The cached object, if it was loaded.

Source

pub async fn get(&self, db: &Db) -> Result<Arc<T>, OrmError>

The referenced object: the cached one, or fetched by primary key (and not cached, since self is borrowed immutably).

§Errors

QueryError::DoesNotExist for a dangling key, or backend errors.

Source

pub fn set_cached(&mut self, object: Arc<T>)

Replace the cached object. Ignored unless its key matches id, so a stale object is never attached.

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

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

Source§

const SQL_TYPE: SqlType = <T::Pk as DbType>::SQL_TYPE

Column type family.
Source§

fn to_value(&self) -> Value

Convert to a bind value.
Source§

fn from_value(value: Value) -> Result<Self, String>

Decode from a value read from any backend. Read more
Source§

const NULLABLE: bool = false

Whether the column accepts NULL (true only for Option<T>).
Source§

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

Source§

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

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

impl<'de, T: Model> Deserialize<'de> for ForeignKey<T>
where T::Pk: Deserialize<'de>,

Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T: Model> Dump for ForeignKey<T>
where T::Pk: Serialize,

§

fn serialize_dump<S>( &self, serializer: S, opts: &DumpOptions, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize directly when supported, preserving dump semantics. Read more
§

fn dump(&self, opts: &DumpOptions) -> Result<Value, DumpError>

Serialize with opts applied. Read more
Source§

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

Source§

fn fk(&mut self) -> Option<&mut ForeignKey<T>>

The foreign key, if set.
Source§

impl<'a, T: Model> Literal<ForeignKey<T>> for &'a T

Source§

fn into_value(self) -> Value

Convert into a bind value.
Source§

impl<T: Model<Pk = i16>> Literal<ForeignKey<T>> for i16

Source§

fn into_value(self) -> Value

Convert into a bind value.
Source§

impl<T: Model<Pk = i32>> Literal<ForeignKey<T>> for i32

Source§

fn into_value(self) -> Value

Convert into a bind value.
Source§

impl<T: Model<Pk = i64>> Literal<ForeignKey<T>> for i64

Source§

fn into_value(self) -> Value

Convert into a bind value.
Source§

impl<T: Model<Pk = Uuid>> Literal<ForeignKey<T>> for Uuid

Source§

fn into_value(self) -> Value

Convert into a bind value.
Source§

impl<T: Model<Pk = String>> Literal<ForeignKey<T>> for String

Source§

fn into_value(self) -> Value

Convert into a bind value.
Source§

impl<'a, T: Model<Pk = String>> Literal<ForeignKey<T>> for &'a str

Source§

fn into_value(self) -> Value

Convert into a bind value.
Source§

impl<'a, T: Model, M> Operand<M, ForeignKey<T>> for &'a T

Source§

fn into_expr(self) -> Expr

Convert into an expression.
Source§

impl<T: Model<Pk = i16>, M> Operand<M, ForeignKey<T>> for i16

Source§

fn into_expr(self) -> Expr

Convert into an expression.
Source§

impl<T: Model<Pk = i32>, M> Operand<M, ForeignKey<T>> for i32

Source§

fn into_expr(self) -> Expr

Convert into an expression.
Source§

impl<T: Model<Pk = i64>, M> Operand<M, ForeignKey<T>> for i64

Source§

fn into_expr(self) -> Expr

Convert into an expression.
Source§

impl<T: Model<Pk = Uuid>, M> Operand<M, ForeignKey<T>> for Uuid

Source§

fn into_expr(self) -> Expr

Convert into an expression.
Source§

impl<T: Model<Pk = String>, M> Operand<M, ForeignKey<T>> for String

Source§

fn into_expr(self) -> Expr

Convert into an expression.
Source§

impl<'a, T: Model<Pk = String>, M> Operand<M, ForeignKey<T>> for &'a str

Source§

fn into_expr(self) -> Expr

Convert into an expression.
Source§

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

Equality compares keys only.

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<T: Model + 'static> Schema for ForeignKey<T>
where T::Pk: Schema + 'static,

Source§

fn schema(registry: &mut SchemaRegistry) -> SchemaObject

The schema definition of this type. Use [SchemaRegistry::subschema] for nested types.
§

fn schema_name() -> Option<&'static str>

Stable component name. Named types are emitted once under components.schemas and referenced with $ref.
Source§

impl<T: Model> Serialize for ForeignKey<T>
where T::Pk: Serialize,

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl<T: Model> Validate for ForeignKey<T>
where T::Pk: Validate,

Source§

fn prepare(input: &mut JsonValue, ctx: &mut ValidationContext)

Check and normalise the raw input for this type in place.
Source§

fn validate(&self, ctx: &mut ValidationContext)

Check invariants of the deserialized value.

Auto Trait Implementations§

§

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

§

impl<T> RefUnwindSafe for ForeignKey<T>
where <T as Model>::Pk: RefUnwindSafe, T: RefUnwindSafe,

§

impl<T> Send for ForeignKey<T>

§

impl<T> Sync for ForeignKey<T>

§

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

§

impl<T> UnsafeUnpin for ForeignKey<T>
where <T as Model>::Pk: UnsafeUnpin,

§

impl<T> UnwindSafe for ForeignKey<T>
where <T as Model>::Pk: UnwindSafe, T: RefUnwindSafe,

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> 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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromValues for T
where T: DbType,

Source§

fn from_values(row: &Row) -> Result<T, QueryError>

Decode row. Read more
§

impl<T> Instrument for T

§

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

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

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> Literal<Option<T>> for T
where T: DbType,

Source§

fn into_value(self) -> Value

Convert into a bind value.
Source§

impl<T> Literal<T> for T
where T: DbType,

Source§

fn into_value(self) -> Value

Convert into a bind value.
Source§

impl<T, M> Operand<M, Option<T>> for T
where T: DbType,

Source§

fn into_expr(self) -> Expr

Convert into an expression.
Source§

impl<T, M> Operand<M, T> for T
where T: DbType,

Source§

fn into_expr(self) -> Expr

Convert into an expression.
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.
§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

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