Skip to main content

toasty_driver_integration_suite/scenarios/
composite_chain_relations.rs

1use crate::prelude::*;
2
3scenario! {
4    /// Multi-relation chain scenario with composite keys at two of the three
5    /// hops:
6    ///
7    /// - `User` has a single-column auto PK.
8    /// - `Todo` has a single-column PK, a single-column FK back to `User`,
9    ///   and a *composite* FK `(category_id, category_revision)` pointing at
10    ///   `Category`.
11    /// - `Category` has a *composite* PK `(id, revision)`.
12    ///
13    /// This lets the chain tests exercise composite-key handling at two
14    /// distinct positions:
15    ///
16    /// - `user.todos().category()` — the second hop is a `BelongsTo` with a
17    ///   composite FK.
18    /// - `category.todos()` — the first hop is a `HasMany` whose paired
19    ///   `BelongsTo` (on `Todo`) is composite.
20    /// - `category.todos().user()` — chains the composite-pair first hop into
21    ///   a single-column second hop.
22
23    #[derive(Debug, toasty::Model)]
24    struct User {
25        #[key]
26        #[auto]
27        id: uuid::Uuid,
28
29        name: String,
30
31        #[has_many]
32        todos: toasty::HasMany<Todo>,
33    }
34
35    #[derive(Debug, toasty::Model)]
36    #[index(category_id, category_revision)]
37    struct Todo {
38        #[key]
39        #[auto]
40        id: uuid::Uuid,
41
42        #[index]
43        user_id: uuid::Uuid,
44
45        #[belongs_to(key = user_id, references = id)]
46        user: toasty::BelongsTo<User>,
47
48        category_id: uuid::Uuid,
49        category_revision: i64,
50
51        #[belongs_to(key = [category_id, category_revision], references = [id, revision])]
52        category: toasty::BelongsTo<Category>,
53
54        title: String,
55    }
56
57    #[derive(Debug, toasty::Model)]
58    #[key(id, revision)]
59    struct Category {
60        id: uuid::Uuid,
61        revision: i64,
62
63        name: String,
64
65        #[has_many]
66        todos: toasty::HasMany<Todo>,
67    }
68
69    async fn setup(test: &mut Test) -> toasty::Db {
70        test.setup_db(models!(User, Todo, Category)).await
71    }
72}