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

Is it possible to have 4 values Composite Key? #352

Closed
videobitva opened this issue Dec 2, 2021 · 15 comments · Fixed by #353 or #423
Closed

Is it possible to have 4 values Composite Key? #352

videobitva opened this issue Dec 2, 2021 · 15 comments · Fixed by #353 or #423
Assignees

Comments

@videobitva
Copy link

Hello there!

I was trying to implement a 4 value Composite Key, here is what I got:

...
#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
pub enum PrimaryKey {
    SwitchIdOne,
    PortIdOne,
    SwitchIdTwo,
    PortIdTwo,
}

impl PrimaryKeyTrait for PrimaryKey {
    type ValueType = (String, i32, String, i32);

    fn auto_increment() -> bool {
        false
    }
}
...

Here is what I got in return from cargo check:

error[E0277]: the trait bound `(std::string::String, i32, std::string::String, i32): ValueType` is not satisfied
  --> src/database/switch_link.rs:59:5
   |
59 |     type ValueType = (String, i32, String, i32);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `ValueType` is not implemented for `(std::string::String, i32, std::string::String, i32)`
   |
   = note: required because of the requirements on the impl of `FromValueTuple` for `(std::string::String, i32, std::string::String, i32)`
note: required by a bound in `sea_orm::PrimaryKeyTrait::ValueType`
  --> /Users/beatrice.raw/.cargo/registry/src/github.com-1ecc6299db9ec823/sea-orm-0.4.0/src/entity/primary_key.rs:47:11
   |
47 |         + FromValueTuple
   |           ^^^^^^^^^^^^^^ required by this bound in `sea_orm::PrimaryKeyTrait::ValueType`

error[E0277]: the trait bound `sea_orm::Value: From<(std::string::String, i32, std::string::String, i32)>` is not satisfied
  --> src/database/switch_link.rs:59:5
   |
59 |     type ValueType = (String, i32, String, i32);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<(std::string::String, i32, std::string::String, i32)>` is not implemented for `sea_orm::Value`
   |
   = help: the following implementations were found:
             <sea_orm::Value as From<&'a [u8]>>
             <sea_orm::Value as From<&'a str>>
             <sea_orm::Value as From<JsonValue>>
             <sea_orm::Value as From<NaiveDate>>
           and 19 others
   = note: required because of the requirements on the impl of `Into<sea_orm::Value>` for `(std::string::String, i32, std::string::String, i32)`
   = note: required because of the requirements on the impl of `FromValueTuple` for `(std::string::String, i32, std::string::String, i32)`
note: required by a bound in `sea_orm::PrimaryKeyTrait::ValueType`

If I try to create a Composite Key of 3 values or less everything is fine.
Is there any workaround to have a 4 values Composite Key in SeaORM?

Thank you!

@billy1624
Copy link
Member

Hey @videobitva, welcome and thanks for the report! I will be creating a PR to support up to 6 values composite key. Stay tuned :P

@billy1624
Copy link
Member

billy1624 commented Dec 3, 2021

Hey @videobitva, please check the PR below

You can try it by updating your sea-orm dep as follow

sea-orm = { version = "^0", git = "https://github.com/SeaQL/sea-orm.git", branch = "issues/352", ... }

@videobitva
Copy link
Author

Wow, that was fast. Checked out it just now, and it just works. Thank you!

@billy1624
Copy link
Member

Welcome! Will let you know once it's released

@billy1624
Copy link
Member

billy1624 commented Dec 4, 2021

Fixed on 0.4.1

CC @videobitva

@emmiegit
Copy link
Contributor

emmiegit commented Jan 5, 2022

I am (allegedly at least) on 0.4.2 and I've been getting a panic when I have composite keys with more than 3 columns.

This is my entry from Cargo.lock:

[[package]]
name = "sea-orm"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f594c2a48a3f2c7c911187c67a39d08f63af932801073737358bef0b5f06576"

Am I missing something here?

@billy1624
Copy link
Member

Hey @ammongit, could you show me the error messages?

@billy1624
Copy link
Member

I'm compiling issues/352 on sea-orm 0.4.2 and it compiled without any errors.

sea-orm = { version = "0.4.2", features = [ "sqlx-mysql", "runtime-async-std-native-tls" ]}

@billy1624
Copy link
Member

billy1624 commented Jan 5, 2022

Can you remove the lines below then execute cargo update, finally compile it again?

source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f594c2a48a3f2c7c911187c67a39d08f63af932801073737358bef0b5f06576"

@emmiegit
Copy link
Contributor

emmiegit commented Jan 5, 2022

It's running via Docker. When attempting to insert an ActiveModel with a four-column primary key: (page_connection_missing, columns from_page_id, to_site_id, to_page_slug, connection_type)

[01:45:53] INSERT INTO "page_connection" ("from_page_id", …; rows: 1, elapsed: 1.775ms
    INSERT INTO
      "page_connection" (
        "from_page_id",
        "to_page_id",
        "connection_type",
        "created_at",
        "updated_at",
        "count"
      )
    VALUES
      ($1, $2, $3, $4, $5, $6) RETURNING "from_page_id",
      "to_page_id",
      "connection_type"
The application panicked (crashed).
Message:  The arity cannot be larger than 3
Location: /usr/local/cargo/registry/src/gh.neting.cc-1ecc6299db9ec823/sea-orm-0.4.2/src/entity/active_model.rs:138
Backtrace omitted.
Run with RUST_BACKTRACE=1 environment variable to display it.
Run with RUST_BACKTRACE=full to include source snippets.

The insert here is the prior insertion and shouldn't be related to this I believe.

@billy1624
Copy link
Member

Ahhh... I see. This is another problem

@emmiegit
Copy link
Contributor

emmiegit commented Jan 5, 2022

Can you remove the lines below then execute cargo update, finally compile it again?

source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f594c2a48a3f2c7c911187c67a39d08f63af932801073737358bef0b5f06576"

I haven't rebuilt it in the container, but removing that and re-running cargo update re-adds the same lines. Seems like it only knows 4f594c... as the latest hash.

@billy1624
Copy link
Member

Hey @ammongit, could you try #423?

@billy1624
Copy link
Member

Wait... let me rebase it onto 0.4.2

@emmiegit
Copy link
Contributor

emmiegit commented Jan 5, 2022

The build took a bit, but I'm getting a different (non-Sea-ORM) error now. Looking at the log, it looks like it was able to insert properly.

I had the following in my Cargo.toml:

sea-orm = { git = "https://github.com/SeaQL/sea-orm", branch = "fix-higher-arity-composite-key", features = ["sqlx-postgres", "runtime-async-std-rustls", "macros"], default-features = false }

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