SeaQL / sea-orm

🐚 An async & dynamic ORM for Rust
https://www.sea-ql.org/SeaORM/
Apache License 2.0
6.94k stars 483 forks source link

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

Open garyrob opened 8 months ago

garyrob commented 8 months ago

Description

If I create an sqlite database withid 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"] }


3. 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 );


4. Run sea-orm-cli generate entity -o src/entities -u sqlite://db/sqlite.db
5. Create src/main.rs with the contents:

mod entities;

[tokio::main]

async fn main() {

6. 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, ^^^^^^^^^^^ 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

<!-- Please add a minimal reproducible example under https://github.com/SeaQL/sea-orm/tree/master/issues, and open a PR subsequently. -->

## 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 commented 6 months ago

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

vvolodin commented 6 months ago

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 commented 5 months ago

same problem.

anastasiya1155 commented 3 weeks ago

Any update on this? Also encountering this issue in my project