sea-orm icon indicating copy to clipboard operation
sea-orm copied to clipboard

`ColumnDef` builder functions in `sea-orm-migration` may push additional statements instead of changing them

Open Rudi3 opened this issue 6 months ago • 7 comments

Description

ColumnDef builder functions in sea-orm-migration may push SQL additional statements instead of changing them.

It looks like string(), for example, will add NOT NULL by default. If you add not_null(), you'll get the statement twice. More problematic is null(), as you will get NOT NULL NULL. This behaviour may cause other "multi-statements" that can contain contradictions, too.

Steps to Reproduce

manager
    .create_table(
        Table::create()
            .table(Post::Table)
            .if_not_exists()
            .col(pk_auto(Post::Id))
            // will create SQL containing NOT NULL NOT NULL
            .col(string(Post::Title).not_null())
            // will create SQL containing NOT NULL NULL
            // which will cause an error
            .col(string(Post::Text).null())
    )
    .await

Expected Behavior

manager
    .create_table(
        Table::create()
            .table(Post::Table)
            .if_not_exists()
            .col(pk_auto(Post::Id))
            // will create SQL containing just NOT NULL
            .col(string(Post::Title).not_null())
            // will create SQL containing just NULL
            // which will not cause an error
            .col(string(Post::Text).null())
    )
    .await

Reproduces How Often

Is it always reproducible? yes

Workarounds

Creating the ColumnDef from scratch seems to work fine:

manager
    .create_table(
        Table::create()
            .table(Post::Table)
            .if_not_exists()
            .col(pk_auto(Post::Id))
            // will create SQL containing NOT NULL NOT NULL
            .col(string(Post::Title).not_null())
            // will create SQL containing just NULL
            .col(
                ColumnDef::new_with_type(
                    Post::Text,
                    ColumnType::String(StringLen::default())
                )
                .null()
            )
    )
    .await

Versions

  • sea-orm-migration v1.1.0-rc1
  • PostgreSQL 16.3 (linux)

Rudi3 avatar Aug 23 '24 19:08 Rudi3