Repository: SeaQL/sea-query (Rust)

Issue:
ColumnDef `timestamp_with_time_zone_len` generates invalid SQL for PostgreSQL
<!--

Welcome! Thanks for reporting bugs!

Do you want to ask a question? Are you looking for support?
Please ask us on
- Discord: https://discord.com/invite/uCPdDXzbdv
- GitHub Discussion

-->

## Description

As indicated in the title, the generated SQL for `timestamp_with_time_zone_len` columns isn't valid as the precision "argument" is placed after 'with time zone` rather than after 'timestamp'.
For reference, see: <https://www.postgresql.org/docs/current/datatype-datetime.html#DATATYPE-DATETIME-TABLE>

## Steps to Reproduce

Run the following migration:

```rust
use sea_schema::migration::prelude::*;

#[derive(Iden)]
enum Foo {
    Table,
    CreatedAt,
}

pub struct Migration;

impl MigrationName for Migration {
    fn name(&self) -> &str {
        "bug-report"
    }
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(Foo::Table)
                    .col(ColumnDef::new(Foo::CreatedAt).timestamp_with_time_zone_len(0))
                    .to_owned(),
            )
            .await
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        // not relevant
        Ok(())
    }
}
```

### Expected Behavior

Should generate and execute the following SQL:

```sql
CREATE TABLE "foo" (
  [...]
  "created_at" timestamp(0) with time zone,
)
```

### Actual Behavior

```sql
CREATE TABLE "foo" (
  [...]
  "created_at" timestamp with time zone(0),
)

Execution Error: error returned from database: syntax error at or near "("
```

## Versions

<!-- you can get this information from copy and pasting the output of `cargo tree | grep sea-` from the console. Also, please include the database and OS that you are running. -->

```
│   └── sea-orm v0.7.1
│       ├── sea-orm-macros v0.7.0 (proc-macro)
│       ├── sea-query v0.23.0
│       │   ├── sea-query-derive v0.2.0 (proc-macro)
│       ├── sea-strum v0.23.0
│       │   └── sea-strum_macros v0.23.0 (proc-macro)
│   └── sea-schema v0.7.1
│       ├── sea-orm v0.7.1 (*)
│       ├── sea-query v0.22.0
│       │   └── sea-query-derive v0.2.0 (proc-macro) (*)
│       ├── sea-schema-derive v0.1.0 (proc-macro)
```

Running against `PostgreSQL 14.2 on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit`



Discussion:
@scootermon thank you for the report, I create PR with fix: https://github.com/SeaQL/sea-query/pull/286
totally didn't know that. Thanks for reporting



Patch:
diff --git a/src/backend/postgres/table.rs b/src/backend/postgres/table.rs
index adef7cc2c..a8116f88c 100644
--- a/src/backend/postgres/table.rs
+++ b/src/backend/postgres/table.rs
@@ -68,7 +68,7 @@ impl TableBuilder for PostgresQueryBuilder {
                     None => "timestamp".into(),
                 },
                 ColumnType::TimestampWithTimeZone(precision) => match precision {
-                    Some(precision) => format!("timestamp with time zone({})", precision),
+                    Some(precision) => format!("timestamp({}) with time zone", precision),
                     None => "timestamp with time zone".into(),
                 },
                 ColumnType::Time(precision) => match precision {


Test patch:
diff --git a/src/tests_cfg.rs b/src/tests_cfg.rs
index 678ee983e..a8cf965ee 100644
--- a/src/tests_cfg.rs
+++ b/src/tests_cfg.rs
@@ -21,6 +21,7 @@ pub enum Character {
     SizeW,
     SizeH,
     FontId,
+    CreatedAt,
 }
 
 /// A shorthand for [`Character`]
@@ -39,6 +40,7 @@ impl Iden for Character {
                 Self::SizeW => "size_w",
                 Self::SizeH => "size_h",
                 Self::FontId => "font_id",
+                Self::CreatedAt => "created_at",
             }
         )
         .unwrap();
diff --git a/tests/postgres/table.rs b/tests/postgres/table.rs
index e644ea285..0a447781f 100644
--- a/tests/postgres/table.rs
+++ b/tests/postgres/table.rs
@@ -232,6 +232,26 @@ fn create_10() {
     );
 }
 
+#[test]
+fn create_11() {
+    assert_eq!(
+        Table::create()
+            .table(Char::Table)
+            .col(
+                ColumnDef::new(Char::CreatedAt)
+                    .timestamp_with_time_zone_len(0)
+                    .not_null()
+            )
+            .to_string(PostgresQueryBuilder),
+        vec![
+            r#"CREATE TABLE "character" ("#,
+            r#""created_at" timestamp(0) with time zone NOT NULL"#,
+            r#")"#,
+        ]
+        .join(" ")
+    );
+}
+
 #[test]
 fn drop_1() {
     assert_eq!(