toasty_core/schema/app/relation/
belongs_to.rs

1use crate::{
2    schema::app::{FieldId, FieldTy, ForeignKey, Model, ModelId, Schema},
3    stmt,
4};
5
6#[derive(Debug, Clone)]
7pub struct BelongsTo {
8    /// Model that owns the relation
9    pub target: ModelId,
10
11    /// The association's expression type. This is the type the field evaluates
12    /// to from a user's point of view.
13    pub expr_ty: stmt::Type,
14
15    /// The `HasMany` or `HasOne` association that pairs with this
16    pub pair: Option<FieldId>,
17
18    /// The foreign key is a set of primitive fields that match the target's
19    /// primary key.
20    pub foreign_key: ForeignKey,
21}
22
23impl BelongsTo {
24    pub fn target<'a>(&self, schema: &'a Schema) -> &'a Model {
25        schema.model(self.target)
26    }
27}
28
29impl From<BelongsTo> for FieldTy {
30    fn from(value: BelongsTo) -> Self {
31        Self::BelongsTo(value)
32    }
33}