SeaQL / SeaQL/sea-orm

sea-orm-cli generates Option<String> instead of Option<PgLtree>

Open
#2,456 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Area:schema Postgres
Dominant language
Rust
Stars
9.9k
Forks
735
Avg merge
6h 36m
Merged PRs (30d)
8

Description

Description

I am using sea-orm-cli to autogenerate entities from my existing PostgreSQL schema:

sea-orm-cli generate entity \
  --output-dir src/entities \
  --database-url "$DATABASE_URL" \
  --with-serde both \
  --serde-skip-deserializing-primary-key \
  --expanded-format

This worked perfectly fine, until I tried to use a table with an ltree column.

The autogenerated entity is using an Option<String> instead of Option<PgLtree>, which then fails to deserialize with an sqlx error:

DatabaseError(Query(SqlxError(ColumnDecode { index: "\"history\"", source: "mismatched types; Rust type `core::option::Option<alloc::string::String>` (as SQL type `TEXT`) is not compatible with SQL type `ltree`" })))

The entity looks like this:

#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel, Eq, Serialize, Deserialize)]
pub struct Model {
    #[serde(skip_deserializing)]
    pub id: i32,
    pub history: Option<String>,
    pub created_at: Option<DateTimeWithTimeZone>,
    pub updated_at: Option<DateTimeWithTimeZone>,
    pub deleted_at: Option<DateTimeWithTimeZone>,
}

Steps to Reproduce

  1. Create a PostgreSQL database schema with a table using a nullable ltree column
  2. Use sea-orm-cli to convert the schema
  3. Try to query the table
Expected Behavior

sea-orm-cli creates an entity using the PgLtree type. Querying the entity succeeds.

Actual Behavior

sea-orm-cli creates an entity using the String (TEXT) type. Querying the entity fails.

Reproduces How Often

100% of the time.

Workarounds

I have looked into the documentation and tried the various sea-orm-cli options, but none of them made any difference.

I tried manually editing the generated entity to switch String->PgLtree, but then it does not compile:

the trait bound `sea_orm::sqlx::sqlx_postgres::types::PgLTree: std::cmp::Eq` is not satisfied
the trait `std::cmp::Eq` is implemented for `std::option::Option<T>`
required for `std::option::Option<sea_orm::sqlx::sqlx_postgres::types::PgLTree>` to implement `std::cmp::Eq`

Reproducible Example

Versions

Tested with the latest version 1.1.2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the sea-orm-cli generate entity command and trace how PostgreSQL column types are mapped for nullable ltree columns. Compare the generated entity with the reported PgLtree and Eq errors; done means the generated entity uses a compatible type and querying the table succeeds.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, rust
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.