Panic on `load_many` call with different column name on primary key
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 9.9k
- Forks
- 734
- Avg merge
- 6h 36m
- Merged PRs (30d)
- 8
Description
Description
I'm trying to use load_many to load the many side of multiple entities. However, if the many-side structure has the column name different from the struct name, and it is a primary key, it panics, as it tries to obtain the parameter through the column name and not through the struct parameter.
As an example, this is a many side of a relation:
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "stock")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub bakery_id: i32,
#[sea_orm(primary_key, column_name = "cake_name")]
pub cake_id: String,
pub amount: i32,
}
I receive a panic message like
thread 'main' panicked at /home/dmaxter/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sea-orm-0.12.3/src/query/loader.rs:360:37:
Failed at mapping string to column A:1
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
With deeper debugging, I found out that this is triggered by this line:
https://github.com/SeaQL/sea-orm/blob/0.12.3/src/query/loader.rs#L226
For this specific case, I found out that here the value is cake_name while the model given only has cake_id, not cake_name property
Steps to Reproduce
- Create a postgres database named bakery
- Fill it with the data on
db.sql - Run the reproducer
Expected Behavior
Obtaining the many side
Actual Behavior
Panic
Reproduces How Often
Always
Workarounds
None, besides renaming the columns to match the model parameters (however this might not be possible, as is the case, because a migration from another platform is happening)
Reproducible Example
Versions
├── sea-orm v0.12.3
│ ├── sea-orm-macros v0.12.3 (proc-macro)
│ │ ├── sea-bae v0.2.0 (proc-macro)
│ ├── sea-query v0.30.1
│ ├── sea-query-binder v0.5.0
│ │ ├── sea-query v0.30.1 (*)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in src/query/loader.rs at the linked lines around 226 and 358-360, then use the reproducer.zip with the PostgreSQL setup described in the issue. Trace how the primary-key column name is mapped to the model field, and verify that the load_many call obtains the many side without panicking when cake_name maps to cake_id.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- postgres, rust
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100