Repository navigation
Replies: 3 comments
let sql = entity::job::Entity::find()
.join(
JoinType::LeftJoin,
crate::entity::job::Relation::SqlJob.def(),
)
.join(
JoinType::LeftJoin,
crate::entity::job::Relation::DataSyncJob.def(),
)
.join(
JoinType::LeftJoin,
crate::entity::job::Relation::ShellJob.def(),
)
.filter(
Condition::any()
.add(Expr::col(entity::sql_job::Column::Code).like(format!("%{}%", code)))
.add(
Expr::col(entity::data_sync_job::Column::SelectSql)
.like(format!("%{}%", code)),
)
.add(
Expr::col(entity::data_sync_job::Column::PreInsertSql)
.like(format!("%{}%", code)),
)
.add(Expr::col(entity::shell_job::Column::Code).like(format!("%{}%", code))),
)
.build(DbBackend::Postgres)
.to_string();
println!("{}", sql);Generated SQL: The filter condition field in the generated SQL does not have a table prefix added. SELECT "job"."id", "job"."job_id", "job"."user_id", "job"."name", "job"."description", "job"."kind", "job"."language", "job"."schedule", "job"."timezone", "job"."parallelism", "job"."parallelism_pending_strategy", "job"."timeout", "job"."retry_count", "job"."enable", "job"."current", "job"."released", "job"."released_description", "job"."created_at", "job"."released_at" FROM "job" LEFT JOIN "sql_job" ON "job"."id" = "sql_job"."job_version_id" LEFT JOIN "data_sync_job" ON "job"."id" = "data_sync_job"."job_version_id" LEFT JOIN "shell_job" ON "job"."id" = "shell_job"."job_version_id" WHERE "code" LIKE '%1%' OR "select_sql" LIKE '%1%' OR "pre_insert_sql" LIKE '%1%' OR "code" LIKE '%1%' |
let sql = entity::job::Entity::find()
.join(
JoinType::LeftJoin,
crate::entity::job::Relation::SqlJob.def(),
)
.join(
JoinType::LeftJoin,
crate::entity::job::Relation::DataSyncJob.def(),
)
.join(
JoinType::LeftJoin,
crate::entity::job::Relation::ShellJob.def(),
)
.filter(
Condition::any()
.add(
Expr::col((entity::sql_job::Entity, entity::sql_job::Column::Code))
.like(format!("%{}%", code)),
)
.add(
Expr::col((
entity::data_sync_job::Entity,
entity::data_sync_job::Column::SelectSql,
))
.like(format!("%{}%", code)),
)
.add(
Expr::col((
entity::data_sync_job::Entity,
entity::data_sync_job::Column::PreInsertSql,
))
.like(format!("%{}%", code)),
)
.add(
Expr::col((entity::shell_job::Entity, entity::shell_job::Column::Code))
.like(format!("%{}%", code)),
),
)
.build(DbBackend::Postgres)
.to_string();
println!("{}", sql);Using Expr::col((Entity, Column)) can generate correct SQL: SELECT "job"."id", "job"."job_id", "job"."user_id", "job"."name", "job"."description", "job"."kind", "job"."language", "job"."schedule", "job"."timezone", "job"."parallelism", "job"."parallelism_pending_strategy", "job"."timeout", "job"."retry_count", "job"."enable", "job"."current", "job"."released", "job"."released_description", "job"."created_at", "job"."released_at" FROM "job" LEFT JOIN "sql_job" ON "job"."id" = "sql_job"."job_version_id" LEFT JOIN "data_sync_job" ON "job"."id" = "data_sync_job"."job_version_id" LEFT JOIN "shell_job" ON "job"."id" = "shell_job"."job_version_id" WHERE "sql_job"."code" LIKE '%1%' OR "data_sync_job"."select_sql" LIKE '%1%' OR "data_sync_job"."pre_insert_sql" LIKE '%1%' OR "shell_job"."code" LIKE '%1%' |
|
You are using the general sea query API, so you need to manually specify the table name. |
Uh oh!
There was an error while loading. Please reload this page.
Description
I encountered a Query Error: column reference "code" is ambiguous when using the pagination feature on a query that involves multiple LEFT JOINs and a LIKE filter on a column that exists in more than one of the joined tables.
It appears that the SQL generated by sea-orm does not correctly qualify the column names in the WHERE clause, resulting in an ambiguity error.
Steps to Reproduce
Both the "sql_job" and "shell_job" tables have a "code" field. The program will report an error when the query conditions include a code filter condition:
Query Error: error returned from database: column reference "code" is ambiguous at line 846
Expected Behavior
Return job paginations
Actual Behavior
Query Error: error returned from database: column reference "code" is ambiguous at line 846
Reproduces How Often
100%
Workarounds
None known.
Versions
rustc 1.97.0 (2d8144b78 2026-07-07)
sea-orm v2.0.0
PostgreSQL 18.4-alpine
The image packaged based on alpine3.24.1 runs on Debian13
│ │ ├── sea-orm v2.0.0
│ │ │ ├── sea-orm-macros v2.0.0 (proc-macro)
│ │ │ │ ├── sea-bae v0.2.1 (proc-macro)
│ │ │ ├── sea-query v1.0.1
│ │ │ │ ├── sea-query-derive v1.0.0 (proc-macro)
│ ├── sea-orm v2.0.0
│ │ ├── sea-orm-macros v2.0.0 (proc-macro) ()
│ │ ├── sea-query v1.0.1
│ │ │ ├── sea-query-derive v1.0.0 (proc-macro) ()
│ │ ├── sea-query-sqlx v0.9.1
│ │ │ ├── sea-query v1.0.1 ()
├── sea-orm v2.0.0 ()
All reactions