Repository: diesel-rs/diesel (language: rust)

Issue:
error: expected one of `{`, lifetime, or type, found `:`
<!--
If you want to report a bug, we added some points below which help us track down the problem faster.
-->

## Setup

### Versions

- **Rust:** `rustc 1.70.0 (90c541806 2023-05-31)`
- **Diesel:** `diesel 2.1.1`
- **Database:** mysql 
- **Operating System** MacOS Ventura 13.4.1 (M2 Chipset)

### Feature Flags

- **diesel:** mysql, uuid

## Problem Description


### What are you trying to accomplish?
I am trying to create a model to correspond with the schema generated from my migrations, following the guide [here](https://diesel.rs/guides/getting-started) but adapting the instructions for Pg to Mysql.


### What is the expected output?
The model should compile. I started with copying and pasting the attributes defined in the Getting Started guide, and then filled in the proper details for my struct according to the generated schema defined in `schema.rs`.


### What is the actual output?

```
error: expected one of `{`, lifetime, or type, found `:`
 --> server/src/database/models.rs:4:21
  |
4 | #[derive(Queryable, Selectable)]
  |                     ^^^^^^^^^^ expected one of `{`, lifetime, or type
  |
  = note: this error originates in the derive macro `Selectable` (in Nightly builds, run with -Z macro-backtrace for more info)
```

### Are you seeing any additional errors?
There is a separate but probably related error here:
```
error: proc-macro derive produced unparsable tokens
 --> server/src/database/models.rs:4:21
  |
4 | #[derive(Queryable, Selectable)]
  | 
```

### Steps to reproduce

Relevant source (lines 1-7):
```
use diesel::prelude::*;
use uuid::Uuid;

#[derive(Queryable, Selectable)]
#[diesel(table_name = crate::database::schema::account)]
#[diesel(check_for_backend(diesel::mysql::Mysql))]
pub struct Account<'n0, 'n1> {
// ... struct properties are defined here, aligning with schema.rs
}
```

While I cannot share the migration file, or the schema itself, I can point out that it uses the UUID type in MySQL and aligns that with the `Uuid` struct defined by the `uuid` package.

Segment of cargo.toml
```
[dependencies]
prost = "0.11.9"
tonic = "0.9.2"
tokio = { version = "1.28.2", features = ["macros", "rt-multi-thread"] }
tonic-reflection = "0.9.2"
diesel = { version = "2.1.0", features = ["mysql", "uuid"] }
dotenvy = "0.15"
uuid = "1.4.1"

[build-dependencies]
tonic-build = "0.9.2"
```

## Checklist

- [X] I have already looked over the [issue tracker](https://github.com/diesel-rs/diesel/issues) and the [discussion forum](https://github.com/diesel-rs/diesel/discussions) for similar possible closed issues.
<!--
If you are unsure if your issue is a duplicate of an existing issue please link the issue in question here
--> 
- [X] This issue can be reproduced on Rust's stable channel. (Your issue will be
  closed if this is not the case)
- [X] This issue can be reproduced without requiring a third party crate

<!--
Thank you for your submission!  You're helping make Diesel more robust 🎉

We'll try to respond as quickly as possible.
-->



Discussion / hints:
This error appears to be resolved when removing the lifetimes `n0` and `n1`.
Thanks for reporting this bug. 

Can you provide the complete definition of the `Account` type, including all fields? That would make it much easier to reproduce the problem.

Fix patch:
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 051f715a1e2e..6b713b96ff03 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -53,12 +53,12 @@ jobs:
           echo "RUSTFLAGS=--cfg doc_cfg" >> $GITHUB_ENV
           echo "RUSTDOCFLAGS=--cfg doc_cfg" >> $GITHUB_ENV
 
-      - name: Set environment variables
-        shell: bash
-        if: matrix.rust != 'nightly'
-        run: |
-          echo "RUSTFLAGS=-D warnings" >> $GITHUB_ENV
-          echo "RUSTDOCFLAGS=-D warnings" >> $GITHUB_ENV
+          #      - name: Set environment variables
+          #shell: bash
+          #if: matrix.rust != 'nightly'
+          #run: |
+          #echo "RUSTFLAGS=-D warnings" >> $GITHUB_ENV
+          #echo "RUSTDOCFLAGS=-D warnings" >> $GITHUB_ENV
 
       - name: Install postgres (Linux)
         if: runner.os == 'Linux' && matrix.backend == 'postgres'
@@ -191,6 +191,8 @@ jobs:
           echo "MYSQL_EXAMPLE_DATABASE_URL=mysql://root@localhost/diesel_example" >> $GITHUB_ENV
           echo "MYSQL_UNIT_TEST_DATABASE_URL=mysql://root@localhost/diesel_unit_test" >> $GITHUB_ENV
           echo "MYSQLCLIENT_LIB_DIR=C:\tools\mysql\current\lib" >> $GITHUB_ENV
+          echo "C:\tools\mysql\current\lib" >> $GITHUB_PATH
+          echo "C:\tools\mysql\current\bin" >> $GITHUB_PATH
 
       - name: Install rust toolchain
         uses: dtolnay/rust-toolchain@master
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6f37cb803d2b..40bf20425b37 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,7 +8,14 @@ default features enabled using some set of dependencies. Those set of dependenci
 an up to date version of the specific dependency. We check this by using the unstable `-Z minimal-version` cargo flag. 
 Increasing the minimal supported Rust version will always be coupled at least with a minor release.
 
-## Unreleased 
+## Unreleased
+
+## [2.1.2] 2023-09-xx
+
+## Fixed
+
+* Fixed another potential breaking chaneg around queries containing `DISTINCT ON` and `ORDER BY` clauses consisting of custom sql expressions (e.g. `.nullable()`)
+* Fixed an issue where `#[derive(Selectable)]` and `#[diesel(check_for_backend)]` generates invalid rust code if the struct contains lifetimes/generic types 
 
 ## [2.1.1] 2023-08-25
 
diff --git a/diesel/Cargo.toml b/diesel/Cargo.toml
index d90b17e8ca48..db7ae3897aff 100644
--- a/diesel/Cargo.toml
+++ b/diesel/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "diesel"
-version = "2.1.1"
+version = "2.1.2"
 license = "MIT OR Apache-2.0"
 description = "A safe, extensible ORM and Query Builder for PostgreSQL, SQLite, and MySQL"
 readme = "README.md"
@@ -36,7 +36,7 @@ itoa = { version = "1.0.0", optional = true }
 time = { version = "0.3.9", optional = true, features = ["macros"] }
 
 [dependencies.diesel_derives]
-version = "~2.1.0"
+version = "~2.1.1"
 path = "../diesel_derives"
 
 [dev-dependencies]
diff --git a/diesel/src/connection/transaction_manager.rs b/diesel/src/connection/transaction_manager.rs
index 506ff5ad3b7c..b30646353e1f 100644
--- a/diesel/src/connection/transaction_manager.rs
+++ b/diesel/src/connection/transaction_manager.rs
@@ -873,7 +873,7 @@ mod test {
             .collect::<Vec<_>>();
 
         results.sort_by_key(|r| r.is_err());
-        assert!(matches!(results[0], Ok(_)), "Got {:?} instead", results);
+        assert!(results[0].is_ok(), "Got {:?} instead", results);
         // Note that contrary to Postgres, this is not a commit failure
         assert!(
             matches!(&results[1], Err(DatabaseError(SerializationFailure, _))),
@@ -982,7 +982,7 @@ mod test {
             .collect::<Vec<_>>();
 
         results.sort_by_key(|r| r.is_err());
-        assert!(matches!(results[0], Ok(_)), "Got {:?} instead", results);
+        assert!(results[0].is_ok(), "Got {:?} instead", results);
         assert!(
             matches!(&results[1], Err(DatabaseError(SerializationFailure, _))),
             "Got {:?} instead",
diff --git a/diesel/src/pg/connection/mod.rs b/diesel/src/pg/connection/mod.rs
index 5e0e766f0d34..bcf9b18f1cfe 100644
--- a/diesel/src/pg/connection/mod.rs
+++ b/diesel/src/pg/connection/mod.rs
@@ -850,7 +850,7 @@ mod tests {
 
         results.sort_by_key(|r| r.is_err());
 
-        assert!(matches!(results[0], Ok(_)), "Got {:?} instead", results);
+        assert!(results[0].is_ok(), "Got {:?} instead", results);
         assert!(
             matches!(&results[1], Err(DatabaseError(SerializationFailure, _))),
             "Got {:?} instead",
@@ -966,7 +966,7 @@ mod tests {
 
         results.sort_by_key(|r| r.is_err());
 
-        assert!(matches!(results[0], Ok(_)), "Got {:?} instead", results);
+        assert!(results[0].is_ok(), "Got {:?} instead", results);
         assert!(
             matches!(&results[1], Err(DatabaseError(SerializationFailure, _))),
             "Got {:?} instead",
diff --git a/diesel/src/pg/query_builder/distinct_on.rs b/diesel/src/pg/query_builder/distinct_on.rs
index 63652575d972..50e7875f7212 100644
--- a/diesel/src/pg/query_builder/distinct_on.rs
+++ b/diesel/src/pg/query_builder/distinct_on.rs
@@ -37,31 +37,84 @@ where
 }
 
 macro_rules! valid_ordering {
-    // Special-case: rule out the all ident case:
+    // Special-case: for single tuple elements
+    // generate plain impls as well:
+    (
+        @optional_untuple:
+        [generics: $($T: ident)*]
+        [distinct: $D:ident]
+        [order: $O: ty,]
+    ) => {
+        // nothing if both a single tuple elements
+    };
+    (
+        @optional_untuple:
+        [generics: $($T: ident)*]
+        [distinct: $D:ident]
+        [order: $($O: ty,)*]
+    ) => {
+        impl<$($T,)*> ValidOrderingForDistinct<DistinctOnClause<$D>>
+            for OrderClause<($($O,)*)>
+        {}
+    };
+    (
+        @optional_untuple:
+        [generics: $($T: ident)*]
+        [distinct: $($D:ident)*]
+        [order: $O: ty,]
+    ) => {
+        impl<$($T,)*> ValidOrderingForDistinct<DistinctOnClause<($($D,)*)>>
+            for OrderClause<$O>
+        {}
+    };
+    (
+        @optional_untuple:
+        [generics: $($T: ident)*]
+        [distinct: $($D:ident)*]
+        [order: $($O: ty,)*]
+    ) => {};
+    // Special-case: rule out the all ident case if the
+    // corresponding flag is set
+    // We want to have these impls if
+    // the tuple sizes do **not** match
+    // therefore we set the flag below
     (@impl_one:
+     [allow_plain = false]
      $generics:tt
+     $distinct:tt
+     $other:tt
      [$($T_:ident, )*]
     ) => {
         /* skip this one */
     };
     (@impl_one:
+     [allow_plain = $allow_plain: expr]
      [generics: $($T:ident)*]
+     [distinct: $($D:ident)*]
+     [other: $($O:ident)*]
      [$($Ty:ty, )*]
     ) => {
-        impl<$($T,)*> ValidOrderingForDistinct<DistinctOnClause<($($T, )*)>>
-            for OrderClause<($($Ty, )*)>
+        impl<$($T,)*> ValidOrderingForDistinct<DistinctOnClause<($($D, )*)>>
+            for OrderClause<($($Ty, )* $($O,)*)>
         {}
+        valid_ordering!(@optional_untuple: [generics: $($T)*] [distinct: $($D)*] [order: $($Ty,)* $($O,)*]);
     };
     (
         @perm:
+        $allow_plain:tt
         $generics:tt
+        $distinct:tt
+        $other:tt
         [acc: $([$($acc:tt)*])*]
         $T:ident
         $($rest:tt)*
     ) => {
         valid_ordering! {
             @perm:
+            $allow_plain
             $generics
+            $distinct
+            $other
                 [acc:
                  $(
                      [$($acc)* crate::expression::operators::Asc<$T>, ]
@@ -74,47 +127,95 @@ macro_rules! valid_ordering {
     };
     (
         @perm:
+        $allow_plain:tt
         $generics:tt
+        $distinct:tt
+        $other:tt
         [acc: $($Tys:tt)*]
         /* nothing left */
     ) => (
         $(
             valid_ordering! {@impl_one:
+                $allow_plain
                 $generics
+                $distinct
+                $other
                 $Tys
             }
         )*
     );
-    (@skip: ($ST1: ident, $($ST:ident,)*), $T1:ident, ) => {};
-    (@skip: ($ST1: ident, $($ST:ident,)*), $T1:ident, $($T:ident,)+) => {
-        valid_ordering!(($($ST,)*), ($ST1,), $($T,)*);
+    (@skip_distinct_rev: [generics: $($G: ident)*] [other: $($O: ident)*] [acc: $($T: ident)*]) => {
+        valid_ordering!(@perm:
+                        [allow_plain = true]
+                        [generics: $($G)*]
+                        [distinct: $($T)*]
+                        [other: $($O)* ]
+                        [acc: []]
+                        $($T)*
+        );
     };
-    (($ST1: ident,), ($($OT:ident,)*), $T1:ident,) => {
-        impl<$T1, $ST1, $($OT,)*> ValidOrderingForDistinct<DistinctOnClause<($ST1, $($OT,)*)>> for OrderClause<$T1>
-        where $T1: crate::pg::OrderDecorator<Column = $ST1>,
-        {}
-        impl<$T1, $ST1, $($OT,)*> ValidOrderingForDistinct<DistinctOnClause<($ST1, $($OT,)*)>> for OrderClause<($T1,)>
-        where $T1: crate::pg::OrderDecorator<Column = $ST1>,
-        {}
-        impl<$T1, $ST1, $($OT,)*> ValidOrderingForDistinct<DistinctOnClause<($T1,)>> for OrderClause<($ST1, $($OT,)*)>
-        where $ST1: crate::pg::OrderDecorator<Column = $T1>,
-        {}
-
-        impl<$T1, $ST1, $($OT,)*> ValidOrderingForDistinct<DistinctOnClause<$T1>> for OrderClause<($ST1, $($OT,)*)>
-        where $ST1: crate::pg::OrderDecorator<Column = $T1>,
-              $T1: crate::Column,
-        {}
+    (@skip_distinct_rev: [generics: $($G: ident)*] [other: $($O: ident)*] [acc: $($I: ident)*] $T: ident $($Ts: ident)*) => {
+        valid_ordering!(
+            @skip_distinct_rev:
+            [generics: $($G)*]
+            [other: $($O)*]
+            [acc: $T $($I)*]
+            $($Ts)*
+        );
     };
-    (($ST1: ident, $($ST:ident,)*), ($($OT: ident,)*), $T1:ident, $($T:ident,)+) => {
-        impl<$T1, $($T,)* $ST1, $($ST,)* $($OT,)*> ValidOrderingForDistinct<DistinctOnClause<($ST1, $($ST,)* $($OT,)*)>> for OrderClause<($T1, $($T,)*)>
-        where $T1: crate::pg::OrderDecorator<Column = $ST1>,
-              $($T: crate::pg::OrderDecorator<Column = $ST>,)*
-        {}
-        impl<$T1, $($T,)* $ST1, $($ST,)* $($OT,)*> ValidOrderingForDistinct<DistinctOnClause<($T1, $($T,)*)>> for OrderClause<($ST1, $($ST,)* $($OT,)*)>
-        where $ST1: crate::pg::OrderDecorator<Column = $T1>,
-              $($ST: crate::pg::OrderDecorator<Column = $T>,)*
-        {}
-        valid_ordering!(($($ST,)*), ($($OT,)* $ST1,), $($T,)*);
+    (@skip_distinct:
+     [generics: $($G: ident)*]
+     [acc: $($O: ident)*]
+     $T: ident
+    ) => {};
+    (@skip_distinct:
+     [generics: $($G: ident)*]
+     [acc: $($O: ident)*]
+     $T:ident $($Ts: ident)*
+    ) => {
+        valid_ordering!(@skip_distinct_rev:
+            [generics: $($G)*]
+            [other: $($O)* $T]
+            [acc: ]
+            $($Ts)*
+        );
+        valid_ordering!(@skip_distinct: [generics: $($G)*] [acc: $($O)* $T] $($Ts)*);
+    };
+    (@skip_order_rev: [generics: $($G: ident)*] [acc: $($T: ident)*]) => {
+        valid_ordering!(@perm:
+            [allow_plain = true]
+            [generics: $($G)*]
+            [distinct: $($G)*]
+            [other: ]
+            [acc: []]
+            $($T)*
+        );
+    };
+    (@skip_order_rev: [generics: $($G: ident)*] [acc: $($I: ident)*] $T: ident $($Ts: ident)*) => {
+        valid_ordering!(
+            @skip_order_rev:
+            [generics: $($G)*]
+            [acc: $T $($I)*]
+            $($Ts)*
+        );
+    };
+    (@skip_order:
+     [generics: $($G: ident)*]
+     $T: ident
+    ) => {};
+    (@skip_order:
+     [generics: $($G: ident)*]
+     $T: ident $($Ts: ident)*
+    ) => {
+        valid_ordering!(@skip_order_rev: [generics: $($G)*] [acc: ] $($Ts)*);
+        valid_ordering!(@skip_order: [generics: $($G)*] $($Ts)*);
+    };
+    (@reverse_list: [generics: $($G: ident)*] [acc: $($I: ident)*]) => {
+        valid_ordering!(@skip_order: [generics: $($G)*] $($I)*);
+        valid_ordering!(@skip_distinct: [generics: $($G)*] [acc: ] $($I)*);
+    };
+    (@reverse_list: [generics: $($G: ident)*] [acc: $($I: ident)*] $T: ident $($Ts: ident)*) => {
+        valid_ordering!(@reverse_list: [generics: $($G)*] [acc: $T $($I)*] $($Ts)*);
     };
     ($(
         $Tuple:tt {
@@ -122,8 +223,15 @@ macro_rules! valid_ordering {
         }
     )+) => {
         $(
-            valid_ordering!(@skip: ($($ST,)*), $($T,)*);
-            valid_ordering!(@perm: [generics: $($T)*] [acc: []] $($T)*);
+            valid_ordering!(@perm:
+                [allow_plain = false]
+                [generics: $($T)*]
+                [distinct: $($T)*]
+                [other: ]
+                [acc: []]
+                $($T)*
+            );
+            valid_ordering!(@reverse_list: [generics: $($T)*] [acc: ] $($T)*);
         )*
     }
 }
@@ -132,7 +240,7 @@ macro_rules! valid_ordering {
 // If we would generate these impls up to max_table_column_count tuple elements that
 // would be a really large number for 128 tuple elements (~64k trait impls)
 // It's fine to increase this number at some point in the future gradually
-diesel_derives::__diesel_for_each_tuple!(valid_ordering, 5);
+diesel_derives::__diesel_for_each_tuple!(valid_ordering, 3);
 
 /// A decorator trait for `OrderClause`
 /// It helps to have bounds on either Col, Asc<Col> and Desc<Col>.
diff --git a/diesel/src/pg/types/date_and_time/chrono.rs b/diesel/src/pg/types/date_and_time/chrono.rs
index 7a50741a4af5..6dc9bef95485 100644
--- a/diesel/src/pg/types/date_and_time/chrono.rs
+++ b/diesel/src/pg/types/date_and_time/chrono.rs
@@ -65,7 +65,7 @@ impl ToSql<Timestamptz, Pg> for NaiveDateTime {
 impl FromSql<Timestamptz, Pg> for DateTime<Utc> {
     fn from_sql(bytes: PgValue<'_>) -> deserialize::Result<Self> {
         let naive_date_time = <NaiveDateTime as FromSql<Timestamptz, Pg>>::from_sql(bytes)?;
-        Ok(DateTime::from_utc(naive_date_time, Utc))
+        Ok(Utc.from_utc_datetime(&naive_date_time))
     }
 }
 
diff --git a/diesel/src/query_builder/query_id.rs b/diesel/src/query_builder/query_id.rs
index 064b41d132a6..846dba5f3d35 100644
--- a/diesel/src/query_builder/query_id.rs
+++ b/diesel/src/query_builder/query_id.rs
@@ -136,9 +136,9 @@ mod tests {
     }
 
     #[test]
-    #[cfg(features = "postgres")]
+    #[cfg(feature = "postgres")]
     fn boxed_queries_do_not_have_static_query_id() {
-        use pg::Pg;
+        use crate::pg::Pg;
         assert!(query_id(users::table.into_boxed::<Pg>()).is_none());
     }
 }
diff --git a/diesel/src/sqlite/connection/sqlite_value.rs b/diesel/src/sqlite/connection/sqlite_value.rs
index ed2917c98b86..d8e4d5bdfe77 100644
--- a/diesel/src/sqlite/connection/sqlite_value.rs
+++ b/diesel/src/sqlite/connection/sqlite_value.rs
@@ -64,7 +64,7 @@ impl<'row, 'stmt, 'query> SqliteValue<'row, 'stmt, 'query> {
         let s = unsafe {
             let ptr = ffi::sqlite3_value_text(self.value.as_ptr());
             let len = ffi::sqlite3_value_bytes(self.value.as_ptr());
-            let bytes = slice::from_raw_parts(ptr as *const u8, len as usize);
+            let bytes = slice::from_raw_parts(ptr, len as usize);
             // The string is guaranteed to be utf8 according to
             // https://www.sqlite.org/c3ref/value_blob.html
             str::from_utf8_unchecked(bytes)
diff --git a/diesel/src/sqlite/types/date_and_time/chrono.rs b/diesel/src/sqlite/types/date_and_time/chrono.rs
index 5b57bea681fe..3877253a8744 100644
--- a/diesel/src/sqlite/types/date_and_time/chrono.rs
+++ b/diesel/src/sqlite/types/date_and_time/chrono.rs
@@ -195,7 +195,7 @@ impl FromSql<TimestamptzSqlite, Sqlite> for DateTime<Utc> {
         // Fallback on assuming Utc
         let naive_date_time =
             <NaiveDateTime as FromSql<TimestamptzSqlite, Sqlite>>::from_sql(value)?;
-        Ok(DateTime::from_utc(naive_date_time, Utc))
+        Ok(Utc.from_utc_datetime(&naive_date_time))
     }
 }
 
diff --git a/diesel/src/sqlite/types/date_and_time/mod.rs b/diesel/src/sqlite/types/date_and_time/mod.rs
index 3b4a0617873e..09330d0d7c61 100644
--- a/diesel/src/sqlite/types/date_and_time/mod.rs
+++ b/diesel/src/sqlite/types/date_and_time/mod.rs
@@ -153,11 +153,11 @@ mod tests {
     }
 
     fn eq_datetime_utc(left: OffsetDateTime, right: DateTime<Utc>) -> bool {
-        left.unix_timestamp_nanos() == right.timestamp_nanos() as i128
+        left.unix_timestamp_nanos() == right.timestamp_nanos_opt().unwrap() as i128
     }
 
     fn eq_datetime_offset(left: OffsetDateTime, right: DateTime<FixedOffset>) -> bool {
-        left.unix_timestamp_nanos() == right.timestamp_nanos() as i128
+        left.unix_timestamp_nanos() == right.timestamp_nanos_opt().unwrap() as i128
     }
 
     fn create_tables(conn: &mut SqliteConnection) {
diff --git a/diesel/src/sqlite/types/date_and_time/time.rs b/diesel/src/sqlite/types/date_and_time/time.rs
index 41c993f6bb92..3995f39c459e 100644
--- a/diesel/src/sqlite/types/date_and_time/time.rs
+++ b/diesel/src/sqlite/types/date_and_time/time.rs
@@ -360,15 +360,15 @@ mod tests {
 
         let midnight = NaiveTime::from_hms(0, 0, 0).unwrap();
         let query = select(time("00:00:00").eq(midnight));
-        assert_eq!(query.get_result::<bool>(connection).unwrap(), true);
+        assert!(query.get_result::<bool>(connection).unwrap());
 
         let noon = NaiveTime::from_hms(12, 0, 0).unwrap();
         let query = select(time("12:00:00").eq(noon));
-        assert_eq!(query.get_result::<bool>(connection).unwrap(), true);
+        assert!(query.get_result::<bool>(connection).unwrap());
 
         let roughly_half_past_eleven = NaiveTime::from_hms_micro(23, 37, 4, 2200).unwrap();
         let query = select(sql::<Time>("'23:37:04.0022'").eq(roughly_half_past_eleven));
-        assert_eq!(query.get_result::<bool>(connection).unwrap(), true);
+        assert!(query.get_result::<bool>(connection).unwrap());
     }
 
     #[test]
diff --git a/diesel_derives/Cargo.toml b/diesel_derives/Cargo.toml
index c3278b1ce875..69ad560d41b2 100644
--- a/diesel_derives/Cargo.toml
+++ b/diesel_derives/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "diesel_derives"
-version = "2.1.1"
+version = "2.1.2"
 license = "MIT OR Apache-2.0"
 description = "You should not use this crate directly, it is internal to Diesel."
 documentation = "https://diesel.rs/guides/"
diff --git a/diesel_derives/src/selectable.rs b/diesel_derives/src/selectable.rs
index 4d0b92b4c637..b3a73d7beb67 100644
--- a/diesel_derives/src/selectable.rs
+++ b/diesel_derives/src/selectable.rs
@@ -11,7 +11,7 @@ use crate::util::wrap_in_dummy_mod;
 pub fn derive(item: DeriveInput) -> Result<TokenStream> {
     let model = Model::from_item(&item, false, false)?;
 
-    let (_, ty_generics, _) = item.generics.split_for_impl();
+    let (_, ty_generics, original_where_clause) = item.generics.split_for_impl();
 
     let mut generics = item.generics.clone();
     generics
@@ -50,20 +50,23 @@ pub fn derive(item: DeriveInput) -> Result<TokenStream> {
             .filter(|(f, _)| !f.embed())
             .flat_map(|(f, ty)| {
                 backends.iter().map(move |b| {
-                    let field_ty = to_field_ty_bound(f.ty_for_deserialize());
-                    let span = field_ty.span();
-                    quote::quote_spanned! {span =>
+                    let span = f.ty.span();
+                    let field_ty = to_field_ty_bound(f.ty_for_deserialize())?;
+                    Ok(syn::parse_quote_spanned! {span =>
                         #field_ty: diesel::deserialize::FromSqlRow<diesel::dsl::SqlTypeOf<#ty>, #b>
-                    }
+                    })
                 })
-            });
+            })
+            .collect::<Result<Vec<_>>>()?;
+        let where_clause = &mut original_where_clause.cloned();
+        let where_clause = where_clause.get_or_insert_with(|| parse_quote!(where));
+        for field_check in field_check_bound {
+            where_clause.predicates.push(field_check);
+        }
         Some(quote::quote! {
-            fn _check_field_compatibility()
-            where
-                #(#field_check_bound,)*
-            {
-
-            }
+            fn _check_field_compatibility #impl_generics()
+                #where_clause
+            {}
         })
     } else {
         None
@@ -87,49 +90,27 @@ pub fn derive(item: DeriveInput) -> Result<TokenStream> {
     }))
 }
 
-fn to_field_ty_bound(field_ty: &syn::Type) -> Option<TokenStream> {
+fn to_field_ty_bound(field_ty: &syn::Type) -> Result<TokenStream> {
     match field_ty {
-        syn::Type::Path(p) => {
-            if let syn::PathArguments::AngleBracketed(ref args) =
-                p.path.segments.last().unwrap().arguments
-            {
-                let lt = args
-                    .args
-                    .iter()
-                    .filter_map(|f| {
-                        if let syn::GenericArgument::Lifetime(lt) = f {
-                            Some(lt)
-                        } else {
-                            None
-                        }
-                    })
-                    .collect::<Vec<_>>();
-                if lt.is_empty() {
-                    Some(quote::quote! {
-                        #field_ty
-                    })
-                } else if lt.len() == args.args.len() {
-                    Some(quote::quote! {
-                        for<#(#lt,)*> #field_ty
-                    })
-                } else {
-                    // type parameters are not supported for checking
-                    // for now
-                    None
-                }
-            } else {
-                Some(quote::quote! {
-                    #field_ty
-                })
-            }
-        }
-        syn::Type::Reference(_r) => {
+        syn::Type::Reference(r) => {
+            use crate::quote::ToTokens;
             // references are not supported for checking for now
             //
             // (How ever you can even have references in a `Queryable` struct anyway)
-            None
+            Err(syn::Error::new(
+                field_ty.span(),
+                format!(
+                    "References are not supported in `Queryable` types\n\
+                         Consider using `std::borrow::Cow<'{}, {}>` instead",
+                    r.lifetime
+                        .as_ref()
+                        .expect("It's a struct field so it must have a named lifetime")
+                        .ident,
+                    r.elem.to_token_stream()
+                ),
+            ))
         }
-        field_ty => Some(quote::quote! {
+        field_ty => Ok(quote::quote! {
             #field_ty
         }),
     }


Test patch:
diff --git a/diesel_cli/tests/migration_generate.rs b/diesel_cli/tests/migration_generate.rs
index 68ea79808485..85126edc6e5e 100644
--- a/diesel_cli/tests/migration_generate.rs
+++ b/diesel_cli/tests/migration_generate.rs
@@ -27,7 +27,7 @@ Creating migrations.\\d{4}-\\d{2}-\\d{2}-\\d{6}_hello.down.sql\
     let captured_timestamps = Regex::new(r"(?P<stamp>[\d-]*)_hello").unwrap();
     let mut stamps_found = 0;
     for caps in captured_timestamps.captures_iter(result.stdout()) {
-        let timestamp = Utc.datetime_from_str(&caps["stamp"], TIMESTAMP_FORMAT);
+        let timestamp = NaiveDateTime::parse_from_str(&caps["stamp"], TIMESTAMP_FORMAT);
         assert!(
             timestamp.is_ok(),
             "Found invalid timestamp format: {:?}",
diff --git a/diesel_compile_tests/tests/fail/derive/selectable.rs b/diesel_compile_tests/tests/fail/derive/selectable.rs
index 5edee02f5912..79c913e9efea 100644
--- a/diesel_compile_tests/tests/fail/derive/selectable.rs
+++ b/diesel_compile_tests/tests/fail/derive/selectable.rs
@@ -41,4 +41,11 @@ struct User {
     no_tuple: i32,
 }
 
+#[derive(Selectable)]
+#[diesel(table_name = users)]
+#[diesel(check_for_backend(diesel::pg::Pg))]
+struct User1<'a> {
+    name: &'a str,
+}
+
 fn main() {}
diff --git a/diesel_compile_tests/tests/fail/derive/selectable.stderr b/diesel_compile_tests/tests/fail/derive/selectable.stderr
index 37dd257c4b05..bfd06d17b43d 100644
--- a/diesel_compile_tests/tests/fail/derive/selectable.stderr
+++ b/diesel_compile_tests/tests/fail/derive/selectable.stderr
@@ -1,3 +1,10 @@
+error: References are not supported in `Queryable` types
+       Consider using `std::borrow::Cow<'a, str>` instead
+  --> tests/fail/derive/selectable.rs:48:11
+   |
+48 |     name: &'a str,
+   |           ^^^^^^^
+
 error[E0412]: cannot find type `non_existing` in module `users`
   --> tests/fail/derive/selectable.rs:26:5
    |
diff --git a/diesel_compile_tests/tests/fail/distinct_on_requires_matching_order_clause.stderr b/diesel_compile_tests/tests/fail/distinct_on_requires_matching_order_clause.stderr
index 1ad308ba0257..70ec21134f4e 100644
--- a/diesel_compile_tests/tests/fail/distinct_on_requires_matching_order_clause.stderr
+++ b/diesel_compile_tests/tests/fail/distinct_on_requires_matching_order_clause.stderr
@@ -7,14 +7,14 @@ error[E0277]: the trait bound `diesel::query_builder::order_clause::OrderClause<
     |                                              required by a bound introduced by this call
     |
     = help: the following other types implement trait `query_dsl::order_dsl::ValidOrderingForDistinct<D>`:
-              <diesel::query_builder::order_clause::OrderClause<(ST1, ST0)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T1,)>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST1, ST0)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<T1>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST1, ST2, ST0)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T1, T2)>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST1, ST2, ST3, ST0)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T1, T2, T3)>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST1, ST2, ST3, ST4, ST0)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T1, T2, T3, T4)>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST2, ST0, ST1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T2,)>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST2, ST0, ST1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<T2>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST2, ST3, ST0, ST1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T2, T3)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T,)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<T>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1, T2)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0,)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<T0>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1, T2)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1, diesel::expression::operators::Asc<T2>)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1, T2)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1, diesel::expression::operators::Desc<T2>)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1, T2)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T2, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0,)>>>
             and $N others
     = note: required for `SelectStatement<FromClause<table>, DefaultSelectClause<FromClause<table>>, NoDistinctClause, ..., ...>` to implement `DistinctOnDsl<columns::name>`
 note: required by a bound in `diesel::QueryDsl::distinct_on`
@@ -23,23 +23,55 @@ note: required by a bound in `diesel::QueryDsl::distinct_on`
     |         Self: methods::DistinctOnDsl<Expr>,
     |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `QueryDsl::distinct_on`
 
-error[E0271]: type mismatch resolving `<id as OrderDecorator>::Column == name`
-   --> tests/fail/distinct_on_requires_matching_order_clause.rs:117:61
+error[E0277]: the trait bound `diesel::query_builder::order_clause::OrderClause<(columns::id, columns::name)>: query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<columns::name>>` is not satisfied
+   --> tests/fail/distinct_on_requires_matching_order_clause.rs:117:73
     |
 117 |     let _ = users::table.order_by((users::id, users::name)).distinct_on(users::name);
-    |                                                             ^^^^^^^^^^^ expected struct `id`, found struct `name`
+    |                                                             ----------- ^^^^^^^^^^^ the trait `query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<columns::name>>` is not implemented for `diesel::query_builder::order_clause::OrderClause<(columns::id, columns::name)>`
+    |                                                             |
+    |                                                             required by a bound introduced by this call
     |
-    = note: required for `diesel::query_builder::order_clause::OrderClause<(columns::id, columns::name)>` to implement `query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<columns::name>>`
+    = help: the following other types implement trait `query_dsl::order_dsl::ValidOrderingForDistinct<D>`:
+              <diesel::query_builder::order_clause::OrderClause<(T,)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<T>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1, T2)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0,)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<T0>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1, T2)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1, diesel::expression::operators::Asc<T2>)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1, T2)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1, diesel::expression::operators::Desc<T2>)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1, T2)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T2, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0,)>>>
+            and $N others
     = note: required for `SelectStatement<FromClause<table>, DefaultSelectClause<FromClause<table>>, NoDistinctClause, ..., ...>` to implement `DistinctOnDsl<columns::name>`
+note: required by a bound in `diesel::QueryDsl::distinct_on`
+   --> $DIESEL/src/query_dsl/mod.rs
+    |
+    |         Self: methods::DistinctOnDsl<Expr>,
+    |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `QueryDsl::distinct_on`
 
-error[E0271]: type mismatch resolving `<id as OrderDecorator>::Column == name`
-   --> tests/fail/distinct_on_requires_matching_order_clause.rs:123:10
+error[E0277]: the trait bound `diesel::query_builder::order_clause::OrderClause<(columns::id, columns::name)>: query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<columns::name>>` is not satisfied
+   --> tests/fail/distinct_on_requires_matching_order_clause.rs:123:22
     |
 123 |         .distinct_on(users::name);
-    |          ^^^^^^^^^^^ expected struct `id`, found struct `name`
+    |          ----------- ^^^^^^^^^^^ the trait `query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<columns::name>>` is not implemented for `diesel::query_builder::order_clause::OrderClause<(columns::id, columns::name)>`
+    |          |
+    |          required by a bound introduced by this call
     |
-    = note: required for `diesel::query_builder::order_clause::OrderClause<(columns::id, columns::name)>` to implement `query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<columns::name>>`
+    = help: the following other types implement trait `query_dsl::order_dsl::ValidOrderingForDistinct<D>`:
+              <diesel::query_builder::order_clause::OrderClause<(T,)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<T>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1, T2)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0,)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<T0>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1, T2)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1, diesel::expression::operators::Asc<T2>)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1, T2)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1, diesel::expression::operators::Desc<T2>)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1, T2)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T2, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0,)>>>
+            and $N others
     = note: required for `SelectStatement<FromClause<table>, DefaultSelectClause<FromClause<table>>, NoDistinctClause, ..., ...>` to implement `DistinctOnDsl<columns::name>`
+note: required by a bound in `diesel::QueryDsl::distinct_on`
+   --> $DIESEL/src/query_dsl/mod.rs
+    |
+    |         Self: methods::DistinctOnDsl<Expr>,
+    |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `QueryDsl::distinct_on`
 
 error[E0277]: the trait bound `diesel::query_builder::order_clause::OrderClause<columns::id>: query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<columns::name>>` is not satisfied
    --> tests/fail/distinct_on_requires_matching_order_clause.rs:127:60
@@ -50,14 +82,14 @@ error[E0277]: the trait bound `diesel::query_builder::order_clause::OrderClause<
     |                                                   required by a bound introduced by this call
     |
     = help: the following other types implement trait `query_dsl::order_dsl::ValidOrderingForDistinct<D>`:
-              <diesel::query_builder::order_clause::OrderClause<(ST1, ST0)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T1,)>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST1, ST0)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<T1>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST1, ST2, ST0)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T1, T2)>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST1, ST2, ST3, ST0)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T1, T2, T3)>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST1, ST2, ST3, ST4, ST0)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T1, T2, T3, T4)>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST2, ST0, ST1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T2,)>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST2, ST0, ST1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<T2>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST2, ST3, ST0, ST1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T2, T3)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T,)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<T>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1, T2)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0,)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<T0>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1, T2)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1, diesel::expression::operators::Asc<T2>)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1, T2)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1, diesel::expression::operators::Desc<T2>)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1, T2)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T2, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0,)>>>
             and $N others
     = note: required for `SelectStatement<FromClause<table>, DefaultSelectClause<FromClause<table>>, DistinctOnClause<name>>` to implement `OrderDsl<columns::id>`
 note: required by a bound in `order_by`
@@ -75,14 +107,14 @@ error[E0277]: the trait bound `diesel::query_builder::order_clause::OrderClause<
     |                                              required by a bound introduced by this call
     |
     = help: the following other types implement trait `query_dsl::order_dsl::ValidOrderingForDistinct<D>`:
-              <diesel::query_builder::order_clause::OrderClause<(ST1, ST0)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T1,)>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST1, ST0)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<T1>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST1, ST2, ST0)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T1, T2)>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST1, ST2, ST3, ST0)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T1, T2, T3)>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST1, ST2, ST3, ST4, ST0)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T1, T2, T3, T4)>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST2, ST0, ST1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T2,)>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST2, ST0, ST1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<T2>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST2, ST3, ST0, ST1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T2, T3)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T,)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<T>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1, T2)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0,)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<T0>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1, T2)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1, diesel::expression::operators::Asc<T2>)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1, T2)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1, diesel::expression::operators::Desc<T2>)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1, T2)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T2, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0,)>>>
             and $N others
     = note: required for `SelectStatement<FromClause<table>, DefaultSelectClause<FromClause<table>>, NoDistinctClause, ..., ...>` to implement `DistinctOnDsl<columns::name>`
 note: required by a bound in `diesel::QueryDsl::distinct_on`
@@ -91,14 +123,30 @@ note: required by a bound in `diesel::QueryDsl::distinct_on`
     |         Self: methods::DistinctOnDsl<Expr>,
     |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `QueryDsl::distinct_on`
 
-error[E0271]: type mismatch resolving `<id as OrderDecorator>::Column == name`
-   --> tests/fail/distinct_on_requires_matching_order_clause.rs:136:10
+error[E0277]: the trait bound `diesel::query_builder::order_clause::OrderClause<(columns::id, columns::name)>: query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<columns::name>>` is not satisfied
+   --> tests/fail/distinct_on_requires_matching_order_clause.rs:136:22
     |
 136 |         .distinct_on(users::name)
-    |          ^^^^^^^^^^^ expected struct `id`, found struct `name`
+    |          ----------- ^^^^^^^^^^^ the trait `query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<columns::name>>` is not implemented for `diesel::query_builder::order_clause::OrderClause<(columns::id, columns::name)>`
+    |          |
+    |          required by a bound introduced by this call
     |
-    = note: required for `diesel::query_builder::order_clause::OrderClause<(columns::id, columns::name)>` to implement `query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<columns::name>>`
+    = help: the following other types implement trait `query_dsl::order_dsl::ValidOrderingForDistinct<D>`:
+              <diesel::query_builder::order_clause::OrderClause<(T,)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<T>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1, T2)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0,)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<T0>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1, T2)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1, diesel::expression::operators::Asc<T2>)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1, T2)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1, diesel::expression::operators::Desc<T2>)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1, T2)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T2, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0,)>>>
+            and $N others
     = note: required for `SelectStatement<FromClause<table>, DefaultSelectClause<FromClause<table>>, NoDistinctClause, ..., ...>` to implement `DistinctOnDsl<columns::name>`
+note: required by a bound in `diesel::QueryDsl::distinct_on`
+   --> $DIESEL/src/query_dsl/mod.rs
+    |
+    |         Self: methods::DistinctOnDsl<Expr>,
+    |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `QueryDsl::distinct_on`
 
 error[E0277]: the trait bound `diesel::query_builder::order_clause::OrderClause<(columns::id, columns::name)>: query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(columns::name, columns::id)>>` is not satisfied
    --> tests/fail/distinct_on_requires_matching_order_clause.rs:143:22
@@ -109,14 +157,14 @@ error[E0277]: the trait bound `diesel::query_builder::order_clause::OrderClause<
     |          required by a bound introduced by this call
     |
     = help: the following other types implement trait `query_dsl::order_dsl::ValidOrderingForDistinct<D>`:
-              <diesel::query_builder::order_clause::OrderClause<(ST1, ST0)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T1,)>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST1, ST0)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<T1>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST1, ST2, ST0)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T1, T2)>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST1, ST2, ST3, ST0)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T1, T2, T3)>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST1, ST2, ST3, ST4, ST0)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T1, T2, T3, T4)>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST2, ST0, ST1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T2,)>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST2, ST0, ST1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<T2>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST2, ST3, ST0, ST1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T2, T3)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T,)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<T>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1, T2)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0,)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<T0>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1, T2)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1, diesel::expression::operators::Asc<T2>)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1, T2)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1, diesel::expression::operators::Desc<T2>)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1, T2)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T2, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0,)>>>
             and $N others
     = note: required for `SelectStatement<FromClause<table>, DefaultSelectClause<FromClause<table>>, NoDistinctClause, ..., ...>` to implement `DistinctOnDsl<(columns::name, columns::id)>`
 note: required by a bound in `diesel::QueryDsl::distinct_on`
@@ -125,23 +173,55 @@ note: required by a bound in `diesel::QueryDsl::distinct_on`
     |         Self: methods::DistinctOnDsl<Expr>,
     |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `QueryDsl::distinct_on`
 
-error[E0271]: type mismatch resolving `<id as OrderDecorator>::Column == name`
-   --> tests/fail/distinct_on_requires_matching_order_clause.rs:150:10
+error[E0277]: the trait bound `diesel::query_builder::order_clause::OrderClause<columns::id>: query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(columns::name, columns::id)>>` is not satisfied
+   --> tests/fail/distinct_on_requires_matching_order_clause.rs:150:22
     |
 150 |         .distinct_on((users::name, users::id))
-    |          ^^^^^^^^^^^ expected struct `id`, found struct `name`
+    |          ----------- ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(columns::name, columns::id)>>` is not implemented for `diesel::query_builder::order_clause::OrderClause<columns::id>`
+    |          |
+    |          required by a bound introduced by this call
     |
-    = note: required for `diesel::query_builder::order_clause::OrderClause<columns::id>` to implement `query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(columns::name, columns::id)>>`
+    = help: the following other types implement trait `query_dsl::order_dsl::ValidOrderingForDistinct<D>`:
+              <diesel::query_builder::order_clause::OrderClause<(T,)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<T>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1, T2)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0,)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<T0>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1, T2)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1, diesel::expression::operators::Asc<T2>)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1, T2)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1, diesel::expression::operators::Desc<T2>)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1, T2)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T2, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0,)>>>
+            and $N others
     = note: required for `SelectStatement<FromClause<table>, DefaultSelectClause<FromClause<table>>, NoDistinctClause, ..., ...>` to implement `DistinctOnDsl<(columns::name, columns::id)>`
+note: required by a bound in `diesel::QueryDsl::distinct_on`
+   --> $DIESEL/src/query_dsl/mod.rs
+    |
+    |         Self: methods::DistinctOnDsl<Expr>,
+    |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `QueryDsl::distinct_on`
 
-error[E0271]: type mismatch resolving `<id as OrderDecorator>::Column == name`
-   --> tests/fail/distinct_on_requires_matching_order_clause.rs:157:10
+error[E0277]: the trait bound `diesel::query_builder::order_clause::OrderClause<(columns::id, columns::name)>: query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<columns::name>>` is not satisfied
+   --> tests/fail/distinct_on_requires_matching_order_clause.rs:157:22
     |
 157 |         .distinct_on(users::name)
-    |          ^^^^^^^^^^^ expected struct `id`, found struct `name`
+    |          ----------- ^^^^^^^^^^^ the trait `query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<columns::name>>` is not implemented for `diesel::query_builder::order_clause::OrderClause<(columns::id, columns::name)>`
+    |          |
+    |          required by a bound introduced by this call
     |
-    = note: required for `diesel::query_builder::order_clause::OrderClause<(columns::id, columns::name)>` to implement `query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<columns::name>>`
+    = help: the following other types implement trait `query_dsl::order_dsl::ValidOrderingForDistinct<D>`:
+              <diesel::query_builder::order_clause::OrderClause<(T,)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<T>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1, T2)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0,)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<T0>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1, T2)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1, diesel::expression::operators::Asc<T2>)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1, T2)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1, diesel::expression::operators::Desc<T2>)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1, T2)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T2, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0,)>>>
+            and $N others
     = note: required for `SelectStatement<FromClause<table>, DefaultSelectClause<FromClause<table>>, NoDistinctClause, ..., ...>` to implement `DistinctOnDsl<columns::name>`
+note: required by a bound in `diesel::QueryDsl::distinct_on`
+   --> $DIESEL/src/query_dsl/mod.rs
+    |
+    |         Self: methods::DistinctOnDsl<Expr>,
+    |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `QueryDsl::distinct_on`
 
 error[E0277]: the trait bound `diesel::query_builder::order_clause::OrderClause<columns::id>: query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<columns::name>>` is not satisfied
    --> tests/fail/distinct_on_requires_matching_order_clause.rs:164:19
@@ -152,14 +232,14 @@ error[E0277]: the trait bound `diesel::query_builder::order_clause::OrderClause<
     |          required by a bound introduced by this call
     |
     = help: the following other types implement trait `query_dsl::order_dsl::ValidOrderingForDistinct<D>`:
-              <diesel::query_builder::order_clause::OrderClause<(ST1, ST0)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T1,)>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST1, ST0)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<T1>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST1, ST2, ST0)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T1, T2)>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST1, ST2, ST3, ST0)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T1, T2, T3)>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST1, ST2, ST3, ST4, ST0)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T1, T2, T3, T4)>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST2, ST0, ST1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T2,)>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST2, ST0, ST1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<T2>>>
-              <diesel::query_builder::order_clause::OrderClause<(ST2, ST3, ST0, ST1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T2, T3)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T,)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<T>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1, T2)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0,)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<T0>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1, T2)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1, diesel::expression::operators::Asc<T2>)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1, T2)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T1, diesel::expression::operators::Desc<T2>)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0, T1, T2)>>>
+              <diesel::query_builder::order_clause::OrderClause<(T0, T2, T1)> as query_dsl::order_dsl::ValidOrderingForDistinct<DistinctOnClause<(T0,)>>>
             and $N others
     = note: required for `SelectStatement<FromClause<table>, DefaultSelectClause<FromClause<table>>, DistinctOnClause<name>>` to implement `OrderDsl<columns::id>`
 note: required by a bound in `order_by`
diff --git a/diesel_derives/tests/selectable.rs b/diesel_derives/tests/selectable.rs
index af1c73e4591c..4b151f7fc2af 100644
--- a/diesel_derives/tests/selectable.rs
+++ b/diesel_derives/tests/selectable.rs
@@ -1,3 +1,7 @@
+use std::marker::PhantomData;
+
+use diesel::deserialize::FromSql;
+use diesel::sql_types::Text;
 use diesel::*;
 
 use crate::helpers::connection;
@@ -172,3 +176,50 @@ fn check_for_backend_with_deserialize_as() {
         name: MyString,
     }
 }
+
+#[allow(dead_code)] // that's essentially a compile test
+#[test]
+fn check_with_lifetime_and_type_param() {
+    use std::borrow::Cow;
+    table! {
+        test {
+            id -> Integer,
+            name -> Text,
+        }
+    }
+
+    #[derive(Queryable, Selectable)]
+    #[diesel(table_name = test)]
+    #[diesel(check_for_backend(crate::helpers::TestBackend))]
+    pub struct Account<'n0> {
+        id: i32,
+        name: Cow<'n0, str>,
+    }
+
+    #[derive(Queryable, Selectable)]
+    #[diesel(table_name = test)]
+    #[diesel(check_for_backend(crate::helpers::TestBackend))]
+    pub struct Foo<T>
+    where
+        T: Copy,
+    {
+        name: FooInner<T>,
+    }
+
+    #[derive(FromSqlRow)]
+    pub struct FooInner<T>(String, PhantomData<T>);
+
+    impl<T> FromSql<Text, crate::helpers::TestBackend> for FooInner<T>
+    where
+        T: Copy,
+    {
+        fn from_sql(
+            bytes: <crate::helpers::TestBackend as backend::Backend>::RawValue<'_>,
+        ) -> deserialize::Result<Self> {
+            Ok(Self(
+                <String as FromSql<Text, crate::helpers::TestBackend>>::from_sql(bytes)?,
+                PhantomData,
+            ))
+        }
+    }
+}
diff --git a/diesel_tests/tests/distinct.rs b/diesel_tests/tests/distinct.rs
index 3e4866091270..de7a9acd6151 100644
--- a/diesel_tests/tests/distinct.rs
+++ b/diesel_tests/tests/distinct.rs
@@ -270,10 +270,54 @@ fn distinct_of_multiple_columns() {
         .load(&mut connection);
     let expected = vec![
         (posts[1].clone(), sean.clone()),
-        (posts[0].clone(), sean),
+        (posts[0].clone(), sean.clone()),
         (posts[5].clone(), tess.clone()),
-        (posts[4].clone(), tess),
+        (posts[4].clone(), tess.clone()),
+    ];
+
+    assert_eq!(Ok(expected), data);
+
+    // with arbitary expressions
+
+    let data = posts::table
+        .left_join(users::table)
+        .order((users::id.nullable(), posts::body.nullable().desc()))
+        .distinct_on((users::id.nullable(), posts::body.nullable()))
+        .load(&mut connection);
+
+    let expected = vec![
+        (posts[1].clone(), Some(sean.clone())),
+        (posts[0].clone(), Some(sean.clone())),
+        (posts[7].clone(), Some(tess.clone())),
+        (posts[6].clone(), Some(tess.clone())),
+    ];
+
+    assert_eq!(Ok(expected), data);
+
+    let data = posts::table
+        .left_join(users::table)
+        .order((users::id.nullable(), posts::body.nullable().desc()))
+        .distinct_on(users::id.nullable())
+        .load(&mut connection);
+
+    let expected = vec![
+        (posts[1].clone(), Some(sean.clone())),
+        (posts[7].clone(), Some(tess.clone())),
     ];
 
     assert_eq!(Ok(expected), data);
+
+    let data = posts::table
+        .left_join(users::table)
+        .order(users::id.nullable())
+        .distinct_on((users::id.nullable(), posts::body.nullable()))
+        .load(&mut connection);
+
+    let expected = vec![
+        (posts[0].clone(), Some(sean.clone())),
+        (posts[1].clone(), Some(sean)),
+        (posts[4].clone(), Some(tess.clone())),
+        (posts[7].clone(), Some(tess)),
+    ];
+    assert_eq!(Ok(expected), data);
 }
diff --git a/diesel_tests/tests/schema_inference.rs b/diesel_tests/tests/schema_inference.rs
index 501ab81d46f4..d8501d19f9c6 100644
--- a/diesel_tests/tests/schema_inference.rs
+++ b/diesel_tests/tests/schema_inference.rs
@@ -229,7 +229,7 @@ mod postgres {
             ts: (Bound::Included(dt), Bound::Unbounded),
             tstz: (
                 Bound::Unbounded,
-                Bound::Excluded(DateTime::<Utc>::from_utc(dt, Utc)),
+                Bound::Excluded(Utc.from_utc_datetime(&dt)),
             ),
             date: (Bound::Included(dt.date()), Bound::Unbounded),
         };
diff --git a/diesel_tests/tests/types_roundtrip.rs b/diesel_tests/tests/types_roundtrip.rs
index 1e10305ee303..746d50812beb 100644
--- a/diesel_tests/tests/types_roundtrip.rs
+++ b/diesel_tests/tests/types_roundtrip.rs
@@ -367,7 +367,7 @@ mod pg_types {
     }
 
     pub fn mk_datetime(data: (i64, u32)) -> DateTime<Utc> {
-        DateTime::from_utc(mk_pg_naive_datetime(data), Utc)
+        Utc.from_utc_datetime(&mk_pg_naive_datetime(data))
     }
 }