diesel/query_dsl/belonging_to_dsl.rs
1/// Constructs a query that finds record(s) based on directional association with other record(s).
2///
3/// # Example
4///
5/// ```rust
6/// # include!("../doctest_setup.rs");
7/// # use schema::{posts, users};
8/// #
9/// # #[derive(Identifiable, Queryable)]
10/// # pub struct User {
11/// # id: i32,
12/// # name: String,
13/// # }
14/// #
15/// # #[derive(Debug, PartialEq)]
16/// # #[derive(Identifiable, Queryable, Associations)]
17/// # #[diesel(belongs_to(User))]
18/// # pub struct Post {
19/// # id: i32,
20/// # user_id: i32,
21/// # title: String,
22/// # }
23/// #
24/// # // Reading the query result requires reading memory allocated by the
25/// # // native sqlite library, which miri cannot track provenance for.
26/// # #[cfg(miri)]
27/// # fn main() {}
28/// # #[cfg(not(miri))]
29/// # fn main() {
30/// # run_test();
31/// # }
32/// #
33/// # fn run_test() -> QueryResult<()> {
34/// # let connection = &mut establish_connection();
35/// # use self::users::dsl::*;
36/// # use self::posts::dsl::{posts, title};
37/// let sean = users.filter(name.eq("Sean")).first::<User>(connection)?;
38/// let tess = users.filter(name.eq("Tess")).first::<User>(connection)?;
39///
40/// let seans_posts = Post::belonging_to(&sean)
41/// .select(title)
42/// .load::<String>(connection)?;
43/// assert_eq!(vec!["My first post", "About Rust"], seans_posts);
44///
45/// // A vec or slice can be passed as well
46/// let more_posts = Post::belonging_to(&vec![sean, tess])
47/// .select(title)
48/// .load::<String>(connection)?;
49/// assert_eq!(
50/// vec!["My first post", "About Rust", "My first post too"],
51/// more_posts
52/// );
53/// # Ok(())
54/// # }
55/// ```
56pub trait BelongingToDsl<T> {
57 /// The query returned by `belonging_to`
58 type Output;
59
60 /// Get the record(s) belonging to record(s) `other`
61 fn belonging_to(other: T) -> Self::Output;
62}