SeaQL / sea-query

🔱 A dynamic SQL query builder for MySQL, Postgres and SQLite
https://www.sea-ql.org
Other
1.12k stars 184 forks source link

ColumnDef `timestamp_with_time_zone_len` generates invalid SQL for PostgreSQL #285

Closed scootermon closed 2 years ago

scootermon commented 2 years ago

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:

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:

CREATE TABLE "foo" (
  [...]
  "created_at" timestamp(0) with time zone,
)

Actual Behavior

CREATE TABLE "foo" (
  [...]
  "created_at" timestamp with time zone(0),
)

Execution Error: error returned from database: syntax error at or near "("

Versions

│   └── 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

ikrivosheev commented 2 years ago

@scootermon thank you for the report, I create PR with fix: https://github.com/SeaQL/sea-query/pull/286

tyt2y3 commented 2 years ago

totally didn't know that. Thanks for reporting