Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect Quoting of Schema-Qualified Enums in cast_as_quoted #827

Open
mpyw opened this issue Oct 9, 2024 · 2 comments
Open

Incorrect Quoting of Schema-Qualified Enums in cast_as_quoted #827

mpyw opened this issue Oct 9, 2024 · 2 comments

Comments

@mpyw
Copy link

mpyw commented Oct 9, 2024

Description

When using the cast_as_quoted function to cast schema-qualified enums (e.g., schema_name.enum_name), the generated SQL incorrectly quotes the entire identifier as "schema_name.enum_name". Instead, each part of the schema and enum should be quoted separately, like "schema_name"."enum_name", to ensure correctness.

Steps to Reproduce

  1. Define a schema-qualified enum:
    CREATE TYPE schema_name.enum_name AS ENUM ('value1', 'value2');
  2. Use SeaQuery to cast a value using cast_as_quoted:
    use sea_query::{tests_cfg::*, *};
    
    let query = Query::select()
        .expr(Func::cast_as_quoted(
            "some_value",
            Alias::new("schema_name.enum_name"),
            '"'.into(),
        ))
        .to_owned();
  3. Generate the SQL for PostgreSQL:
    assert_eq!(
        query.to_string(PostgresQueryBuilder),
        r#"SELECT CAST('some_value' AS "schema_name.enum_name")"#
    );

Expected Behavior

The SQL should be generated as:

SELECT CAST('some_value' AS "schema_name"."enum_name")

Actual Behavior

The SQL is incorrectly generated as:

SELECT CAST('some_value' AS "schema_name.enum_name")

Reproduces How Often

Always.

Versions

Additional Information

@mpyw
Copy link
Author

mpyw commented Oct 9, 2024

I find it counterintuitive that calling with Alias::new("schema_name.enum_name") results in "schema_name"."enum_name". The identifier should be fully determined at the alias definition stage and should not be modified.

To address this, we could modify cast_as_quoted to also accept Expr, allowing for cases like:

Expr::col((Alias::new("schema_name"), Alias::new("enum_name")))

Additionally, DeriveActiveEnum (sea-orm-macros) definition:

#[sea_orm(
    rs_type = "String",
    db_type = "Enum",
    enum_name = "schema_name.enum_name"
)]

it could be updated to handle dot-chain parsing. Alternatively:

  • Introduce enum_name_raw field could allow for manual quoting:
#[sea_orm(
    rs_type = "String",
    db_type = "Enum",
    enum_name_raw = "Expr::col((Alias::new(\"schema_name\"), Alias::new(\"enum_name\")))"
)]
  • Introduce schema_name:
#[sea_orm(
    rs_type = "String",
    db_type = "Enum",
    schema_name = "schema_name"
    enum_name = "enum_name"
)]

What do you think?

@mpyw
Copy link
Author

mpyw commented Oct 9, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant