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

sea-orm-cli generates uncompilable model from pre-existing autoincrement primary key in sqlite #2051

Open
garyrob opened this issue Jan 4, 2024 · 3 comments

Comments

@garyrob
Copy link

garyrob commented Jan 4, 2024

Description

If I create an sqlite database with id INTEGER PRIMARY KEY AUTOINCREMENT, and then run sea-orm-cli generate entity, I get a

Steps to Reproduce

  1. Initialize a cargo project called test_id. All paths and commands are from the root of that project.

  2. Cargo.toml should contain:

[package]
name = "test_id"
version = "0.1.0"
edition = "2021"

[dependencies]
sea-orm = "0.12.10"  # Check for the latest version
tokio = { version = "1", features = ["full"] }

  1. Create a sqlite3 database in db/sqlite.db with only the following table:
CREATE TABLE IF NOT EXISTS "tasks" (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    task_num INTEGER NOT NULL UNIQUE
);
  1. Run sea-orm-cli generate entity -o src/entities -u sqlite://db/sqlite.db
  2. Create src/main.rs with the contents:
mod entities;

#[tokio::main]
async fn main() {
  1. Run cargo build

Expected Behavior

It's supposed to compile successfully.

Actual Behavior

We get the following error:

   Compiling test_id v0.1.0 (/Users/garyrob/Source/myrust/test_id)
error[E0277]: the trait bound `Option<i32>: sea_orm::ActiveEnum` is not satisfied
  --> src/entities/tasks.rs:9:13
   |
9  |     pub id: Option<i32>,
   |             ^^^^^^^^^^^ the trait `sea_orm::ActiveEnum` is not implemented for `Option<i32>`
   |
   = help: the following other types implement trait `TryFromU64`:
             bool
             i8
             i16
             i32
             i64
             u8
             u16
             u32
           and 29 others
   = note: required for `Option<i32>` to implement `TryFromU64`
note: required by a bound in `sea_orm::PrimaryKeyTrait::ValueType`
  --> /Users/garyrob/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sea-orm-0.12.10/src/entity/primary_key.rs:49:11
   |
42 |     type ValueType: Sized
   |          --------- required by a bound in this associated type
...
49 |         + TryFromU64;
   |           ^^^^^^^^^^ required by this bound in `PrimaryKeyTrait::ValueType`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `test_id` (bin "test_id") due to previous error

Reproduces How Often

Every time.

Workarounds

None known if we want the primary key to be auto-increment.

Reproducible Example

Versions

➜  test_id git:(master) ✗ cargo tree | grep sea-
├── sea-orm v0.12.10
│   ├── sea-orm-macros v0.12.10 (proc-macro)
│   │   ├── sea-bae v0.2.0 (proc-macro)
│   ├── sea-query v0.30.6


@tyt2y3
Copy link
Member

tyt2y3 commented Mar 2, 2024

The missing important bit of information is, what's the Entity being generated like?

@vvolodin
Copy link

vvolodin commented Mar 6, 2024

I'm having the same issue, here's one of my generated models:

//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.14

use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "Category")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = false)]
    pub key: Option<i32>,
    pub name: Option<String>,
    pub icon: Option<String>,
    #[sea_orm(
        column_name = "deleteOK",
        column_type = "Binary(BlobSize::Blob(None))",
        nullable
    )]
    pub delete_ok: Option<Vec<u8>>,
    #[sea_orm(column_name = "seqNum")]
    pub seq_num: Option<i32>,
    #[sea_orm(column_name = "deviceIdKey")]
    pub device_id_key: Option<i32>,
    #[sea_orm(column_name = "deviceKey")]
    pub device_key: Option<i32>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}

The key is typed as Option<i32> and that doesn't compile.

@canxin121
Copy link

same problem.

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

4 participants