Skip to content

Commit c0b6130

Browse files
authored
Merge pull request diesel-rs#2773 from felixwatts/master
improve left_join docs
2 parents adec833 + fd498fb commit c0b6130

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

diesel/src/query_dsl/mod.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,75 @@ pub trait QueryDsl: Sized {
496496
/// instead. See [`inner_join`] for usage examples.
497497
///
498498
/// [`inner_join`]: QueryDsl::inner_join()
499+
///
500+
/// Columns in the right hand table will become `Nullable` which means
501+
/// you must call `nullable()` on the corresponding fields in the select
502+
/// clause:
503+
///
504+
/// ### Selecting after a left join
505+
///
506+
/// ```rust
507+
/// # include!("../doctest_setup.rs");
508+
/// # use schema::{users, posts};
509+
/// #
510+
/// # #[derive(Queryable, PartialEq, Eq, Debug)]
511+
/// # struct User {
512+
/// # id: i32,
513+
/// # name: String,
514+
/// # }
515+
/// #
516+
/// # impl User {
517+
/// # fn new(id: i32, name: &str) -> Self {
518+
/// # User {
519+
/// # id,
520+
/// # name: name.into(),
521+
/// # }
522+
/// # }
523+
/// # }
524+
/// #
525+
/// # #[derive(Queryable, PartialEq, Eq, Debug)]
526+
/// # struct Post {
527+
/// # id: i32,
528+
/// # user_id: i32,
529+
/// # title: String,
530+
/// # }
531+
/// #
532+
/// # impl Post {
533+
/// # fn new(id: i32, user_id: i32, title: &str) -> Self {
534+
/// # Post {
535+
/// # id,
536+
/// # user_id,
537+
/// # title: title.into(),
538+
/// # }
539+
/// # }
540+
/// # }
541+
/// #
542+
/// # fn main() {
543+
/// # run_test().unwrap();
544+
/// # }
545+
/// #
546+
/// # fn run_test() -> QueryResult<()> {
547+
/// # let connection = establish_connection();
548+
/// # connection.execute("DELETE FROM posts")?;
549+
/// # diesel::insert_into(posts::table)
550+
/// # .values((posts::user_id.eq(1), posts::title.eq("Sean's Post")))
551+
/// # .execute(&connection)?;
552+
/// # let post_id = posts::table.select(posts::id)
553+
/// # .first::<i32>(&connection)?;
554+
/// let join = users::table.left_join(posts::table);
555+
///
556+
/// // Since `posts` is on the right side of a left join, `.nullable` is
557+
/// // needed.
558+
/// let names_and_titles = join.select((users::name, posts::title.nullable()))
559+
/// .load::<(String, Option<String>)>(&connection)?;
560+
/// let expected_data = vec![
561+
/// (String::from("Sean"), Some(String::from("Sean's Post"))),
562+
/// (String::from("Tess"), None),
563+
/// ];
564+
/// assert_eq!(expected_data, names_and_titles);
565+
/// # Ok(())
566+
/// # }
567+
/// ```
499568
fn left_outer_join<Rhs>(self, rhs: Rhs) -> LeftJoin<Self, Rhs>
500569
where
501570
Self: JoinWithImplicitOnClause<Rhs, joins::LeftOuter>,

0 commit comments

Comments
 (0)