SeaQL/sea-orm

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

garyrob opened this issue · 3 comments

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


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

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.

same problem.