Skip to main content

RelatedMany

Struct RelatedMany 

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

A collection of related objects (one-to-many or many-to-many).

This wrapper can be in one of two states:

  • Unloaded: the collection has not been fetched yet
  • Loaded: the objects have been fetched and cached

For many-to-many relationships, use link() and unlink() to track changes that will be flushed to the link table.

Implementations§

Source§

impl<T: Model> RelatedMany<T>

Source

pub fn new(fk_column: &'static str) -> Self

Create a new unloaded RelatedMany with the FK column name.

Use this for one-to-many relationships where the related model has a foreign key column pointing back to this model.

Create for a many-to-many relationship with a link table.

§Example
let link = LinkTableInfo::new("hero_powers", "hero_id", "power_id");
let powers: RelatedMany<Power> = RelatedMany::with_link_table(link);
Source

pub fn with_parent_pk(fk_column: &'static str, pk: impl Into<Value>) -> Self

Create with a parent primary key for loading.

Source

pub fn is_loaded(&self) -> bool

Check if the collection has been loaded.

Source

pub fn get(&self) -> Option<&[T]>

Get the loaded objects as a slice (None if not loaded).

Source

pub fn len(&self) -> usize

Get the number of loaded items (0 if not loaded).

Source

pub fn is_empty(&self) -> bool

Check if the collection is empty (true if not loaded or loaded empty).

Source

pub fn set_loaded(&self, objects: Vec<T>) -> Result<(), Vec<T>>

Set the loaded objects (internal use by query system).

Source

pub fn iter(&self) -> impl Iterator<Item = &T>

Iterate over the loaded items.

Source

pub fn fk_column(&self) -> &'static str

Get the FK column name.

Source

pub fn parent_pk(&self) -> Option<&Value>

Get the parent PK value (if set).

Source

pub fn set_parent_pk(&mut self, pk: impl Into<Value>)

Set the parent PK value.

Get the link table info (if this is a many-to-many relationship).

Source

pub fn is_many_to_many(&self) -> bool

Check if this is a many-to-many relationship (has link table).

Track a link operation (will INSERT into link table on flush).

The object should already exist in the database. This method records the relationship to be persisted when flush() is called.

Duplicate links to the same object are ignored (only one INSERT will occur).

§Example
hero.powers.link(&fireball);
session.flush().await?; // Inserts into hero_powers table

Track an unlink operation (will DELETE from link table on flush).

This method records the relationship removal to be persisted when flush() is called.

Duplicate unlinks to the same object are ignored (only one DELETE will occur).

§Example
hero.powers.unlink(&fireball);
session.flush().await?; // Deletes from hero_powers table

Get and clear pending link operations.

Returns the PK values that should be INSERTed into the link table. This is used by the flush system.

Get and clear pending unlink operations.

Returns the PK values that should be DELETEd from the link table. This is used by the flush system.

Source

pub fn has_pending_ops(&self) -> bool

Check if there are pending link/unlink operations.

Trait Implementations§

Source§

impl<T: Model + Clone> Clone for RelatedMany<T>

Source§

fn clone(&self) -> Self

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 + Debug> Debug for RelatedMany<T>

Source§

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

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

impl<T: Model> Default for RelatedMany<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de, T> Deserialize<'de> for RelatedMany<T>
where T: Model + 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<'a, T: Model> IntoIterator for &'a RelatedMany<T>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T> Serialize for RelatedMany<T>
where T: Model + Serialize,

Source§

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

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for RelatedMany<T>

§

impl<T> RefUnwindSafe for RelatedMany<T>

§

impl<T> Send for RelatedMany<T>

§

impl<T> Sync for RelatedMany<T>

§

impl<T> Unpin for RelatedMany<T>
where T: Unpin,

§

impl<T> UnwindSafe for RelatedMany<T>
where T: UnwindSafe,

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, _span: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
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> ModelDump for T
where T: Serialize,

Source§

fn model_dump(&self, options: DumpOptions) -> Result<Value, Error>

Serialize a model to a JSON value. Read more
Source§

fn model_dump_json(&self) -> Result<String, Error>

Serialize a model to a JSON string with default options.
Source§

fn model_dump_json_pretty(&self) -> Result<String, Error>

Serialize a model to a pretty-printed JSON string.
Source§

fn model_dump_json_with_options( &self, options: DumpOptions, ) -> Result<String, Error>

Serialize a model to a JSON string with full options support. Read more
Source§

impl<T> ModelValidate for T

Source§

fn model_validate( input: impl Into<ValidateInput>, options: ValidateOptions, ) -> Result<T, ValidationError>

Create and validate a model from input. Read more
Source§

fn model_validate_json(json: &str) -> ValidateResult<Self>

Create and validate a model from JSON string with default options.
Source§

fn model_validate_dict(dict: HashMap<String, Value>) -> ValidateResult<Self>

Create and validate a model from a HashMap with default options.
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<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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,