1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//! ForeignKey accessor for reverse relationship access.
//!
//! This module provides the `ForeignKeyAccessor` type, which enables type-safe
//! access to reverse ForeignKey relationships without using string literals.
//!
//! ## Overview
//!
//! When a model has a ForeignKey field, the `#[model(...)]` macro automatically
//! generates a `{field_name}_accessor()` static method that returns a
//! `ForeignKeyAccessor`. This accessor provides a `.reverse()` method to access
//! related records from the target side of the relationship.
//!
//! ## Usage
//!
//! ```rust,ignore
//! use reinhardt_db::orm::ForeignKeyAccessor;
//!
//! // Tweet model has: #[rel(foreign_key)] user: ForeignKeyField<User>
//!
//! // Get a reverse accessor to fetch all tweets for a user
//! let tweets_accessor = Tweet::user_accessor().reverse(&user, db.clone());
//!
//! // Use the accessor to query related records
//! let tweets = tweets_accessor.all().await?;
//! let tweet_count = tweets_accessor.count().await?;
//!
//! // Paginate results
//! let page1 = tweets_accessor.paginate(1, 10).all().await?;
//! ```
//!
//! ## API Design
//!
//! This design provides several advantages:
//! - **Type-safe**: No string literals required
//! - **IDE support**: Full auto-completion for accessor methods
//! - **Compile-time checks**: Invalid relationships cause compilation errors
//! - **Consistent pattern**: Follows the same pattern as ManyToMany accessors
use DatabaseConnection;
use ReverseAccessor;
use crateModel;
use ;
use PhantomData;
/// Accessor for ForeignKey relationships that enables reverse access.
///
/// This type is returned by `{field_name}_accessor()` methods generated
/// by the `#[model(...)]` macro for ForeignKey fields.
///
/// # Type Parameters
///
/// - `Source`: The model containing the ForeignKey field (e.g., Tweet)
/// - `Target`: The model being referenced (e.g., User)
///
/// # Examples
///
/// ```rust,ignore
/// // Tweet has: #[rel(foreign_key)] user: ForeignKeyField<User>
///
/// // Get reverse accessor for User → Tweets relationship
/// let tweets_accessor = Tweet::user_accessor().reverse(&user, db);
/// let tweets = tweets_accessor.all().await?;
/// ```
// Note: Tests for ForeignKeyAccessor require actual model types and database connections.
// Integration tests are in the examples-twitter project which uses real model types.