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

i64 in sqlite: bigint NOT NULL PRIMARY KEY AUTOINCREMENT #1832

Open
GuoMonth opened this issue Aug 30, 2023 · 2 comments
Open

i64 in sqlite: bigint NOT NULL PRIMARY KEY AUTOINCREMENT #1832

GuoMonth opened this issue Aug 30, 2023 · 2 comments

Comments

@GuoMonth
Copy link

GuoMonth commented Aug 30, 2023

Description

I am define entity , this is code:

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "t_test")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i64,
    pub create_user: Option<String>,
    pub create_time: DateTime,
    pub update_user: Option<String>,
    pub update_time: DateTime,

}

Migration code:

impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        let backend = manager.get_database_backend();
        let schema = Schema::new(backend);

        let stmt = schema.create_table_from_entity(TestEntity);
        let r = manager.create_table(stmt).await;
        if let Err(err) = r {
            println!("{}", err);
            return Err(err);
        }

        Ok(())
    }

Steps to Reproduce

  1. run this test code
env::set_var("RUST_LOG", "debug");
tracing_subscriber::fmt::init();
let mut opt = ConnectOptions::new("sqlite:file::memory:?cache=shared");
opt.sqlx_logging_level(log::LevelFilter::Info);
let db = Database::connect(opt.clone()).await.unwrap();
let _ = Migrator::up(&db, None).await;
  1. will get error:
2023-08-30T04:46:55.588146Z DEBUG sea_orm::driver::sqlx_sqlite: CREATE TABLE "t_test" ( "id" bigint NOT NULL PRIMARY KEY AUTOINCREMENT, "create_user" text, "create_time" text NOT NULL, "update_user" text, "update_time" text NOT NULL)

thread 'test_create_test_server' panicked at 'called `Result::unwrap()` on an `Err` value: Exec(SqlxError(Database(SqliteError { code: 1, message: "AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY" })))'

Expected Behavior

It is success, when i use postgresql.
But use sqlite, i am get error. why?

I am read this doc https://www.sea-ql.org/SeaORM/docs/generate-entity/entity-structure/
I think the create table sql is error。
In the doc, the i64 id should be "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT but not "id" bigint NOT NULL PRIMARY KEY AUTOINCREMENT

Actual Behavior

Different the document description
in sqlite,
i64 -> bigint, ❌,different doc
i64 -> integet, expected,and same doc. but actual behavior is bigint

Reproduces How Often

always reproducible!

Versions

Cargo.toml

sea-orm = { version = "0.12.2", features = [
    "sqlx-postgres",
    "sqlx-sqlite",
    "runtime-tokio-rustls",
] }
sea-orm-migration = { version = "0.12.2", features = [
    "sqlx-sqlite",
    "sqlx-postgres",
] }
@tyt2y3
Copy link
Member

tyt2y3 commented Aug 30, 2023

It appears to be a quirkiness of SQLite
Ref: https://sqlite.org/forum/info/2dfa968a702e1506e885cb06d92157d492108b22bf39459506ab9f7125bca7fd
In most cases BIGINT is synonyms with INTEGER, but not in PRIMARY KEY AUTOINCREMENT

I would suggest a workaround for now:

#[sea_orm(primary_key)]
#[cfg_attr(feature = "sqlite", sea_orm(column_type = "Integer"))]
pub id: i64,

@tyt2y3
Copy link
Member

tyt2y3 commented Aug 30, 2023

Created an issue in sea-query, as there is nothing to be done on sea-orm side.

fgaz added a commit to fgaz/attic that referenced this issue Nov 6, 2023
Fixes the following error

> AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY

by applying the workaround from SeaQL/sea-orm#1832
fgaz added a commit to fgaz/attic that referenced this issue Nov 6, 2023
Fixes the following error

> AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY

by applying the workaround from SeaQL/sea-orm#1832
fgaz added a commit to fgaz/attic that referenced this issue Nov 6, 2023
Fixes the following error

> AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY

by applying the workaround from SeaQL/sea-orm#1832
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

2 participants