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>
impl<T: Model> RelatedMany<T>
Sourcepub fn new(fk_column: &'static str) -> Self
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.
Sourcepub fn with_link_table(link_table: LinkTableInfo) -> Self
pub fn with_link_table(link_table: LinkTableInfo) -> Self
Sourcepub fn with_parent_pk(fk_column: &'static str, pk: impl Into<Value>) -> Self
pub fn with_parent_pk(fk_column: &'static str, pk: impl Into<Value>) -> Self
Create with a parent primary key for loading.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Check if the collection is empty (true if not loaded or loaded empty).
Sourcepub fn set_loaded(&self, objects: Vec<T>) -> Result<(), Vec<T>>
pub fn set_loaded(&self, objects: Vec<T>) -> Result<(), Vec<T>>
Set the loaded objects (internal use by query system).
Sourcepub fn set_parent_pk(&mut self, pk: impl Into<Value>)
pub fn set_parent_pk(&mut self, pk: impl Into<Value>)
Set the parent PK value.
Sourcepub fn link_table(&self) -> Option<&LinkTableInfo>
pub fn link_table(&self) -> Option<&LinkTableInfo>
Get the link table info (if this is a many-to-many relationship).
Sourcepub fn is_many_to_many(&self) -> bool
pub fn is_many_to_many(&self) -> bool
Check if this is a many-to-many relationship (has link table).
Sourcepub fn link(&self, obj: &T)
pub fn link(&self, obj: &T)
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 tableSourcepub fn unlink(&self, obj: &T)
pub fn unlink(&self, obj: &T)
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 tableSourcepub fn take_pending_links(&self) -> Vec<Vec<Value>>
pub fn take_pending_links(&self) -> Vec<Vec<Value>>
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.
Sourcepub fn take_pending_unlinks(&self) -> Vec<Vec<Value>>
pub fn take_pending_unlinks(&self) -> Vec<Vec<Value>>
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.
Sourcepub fn has_pending_ops(&self) -> bool
pub fn has_pending_ops(&self) -> bool
Check if there are pending link/unlink operations.